MongoDB c#驱动程序的字段投影
本文关键字:字段 投影 驱动程序 MongoDB | 更新日期: 2023-09-27 18:02:08
嗨,我在mongoDB中有一个集合,我想从中只获得部分字段,我创建了一个类,我正在向mongoDB插入数据
ClassCode:
public class FrameDocument
{
public ObjectId _id { get; set; }
public Nullable<System.DateTime> FrameTimeStamp { get; set; }
public Nullable<int> ActivePick { get; set; }
public Nullable<int> TraderId { get; set; }
public Nullable<int> EventCodeId { get; set; }
public byte[] Frame { get; set; }
public int ServerUserId { get; set; }
public int SesionId { get; set; }
public string TraderName { get; set; }
public string ServerUserName { get; set; }
}
这是插入代码:
FrameDocument frameDoc = new FrameDocument();
frameDoc.Frame = imageBA;
frameDoc.EventCodeId = 1;
frameDoc.SesionId = 1;
frameDoc.FrameTimeStamp = DateTime.Now;
frameDoc.ServerUserId = (int)toMongoDt.Rows[0]["ServerUserId"];
frameDoc.TraderId = (int)toMongoDt.Rows[0]["TraderId"];
frameDoc.ActivePick = (int)toMongoDt.Rows[0]["ActivePick"];
frameDoc.TraderName = (string)toMongoDt.Rows[0]["TraderName"];
frameDoc.ServerUserName = (string)toMongoDt.Rows[0] ["ServerUserName"];
var mongoCon = "mongodb://127.0.0.1";
MongoClient client = new MongoClient(mongoCon);
var db = client.GetDatabase("Video");
var frameCollection = db.GetCollection<FrameDocument>("Frame");
frameCollection.InsertOne(frameDoc);
**现在我从这个代码集合得到所有的字段,但我想离开类的框架字段,我试图建立不同的类没有这个字段,但我不知道如何不接收框架字段**
var collection = db.GetCollection<BsonDocument>("Frame");
var builder = Builders<BsonDocument>.Filter;
var filter = builder.Eq("SesionId", 1)
& builder.Eq("TraderId", 125)
& builder.Eq("ServerUserId", 1)
& builder.Lt("FrameTimeStamp", sing.eDate)
& builder.Gt("FrameTimeStamp", sing.sDate);
var result = collection.Find(filter).ToList();
有人能帮忙吗?
请看:
_result = _collection.Find(o => o._id == _id)
.Project<FrameDocumentNoFrameField>
(Builders<FrameDocument>.Projection.Exclude(f => f.Frame)).ToList();
其中FrameDocumentNoFrameField是一个没有Frame字段的类
源这里#例子:模型类
public class Company
{
public string CompanyId { get; set; }
public string CompanyName { get; set; }
public List<CompanySettings>{ get; set; }
}
[BsonIgnoreExtraElements]
public class CompanySettings
{
public CompanySetupType CompanySetupTypeId { get; set; }
public List<string> CompanyEmployee{ get; set; }
}
#现在创建一个想要读取值的投影类
[BsonIgnoreExtraElements]
public class CompanySettingsProjectionModel
{
public List<CompanySettings> CompanySettings { get; set; }
}
#创建投影后,使用Builders从mongo获取数据
public async Task<CompanySettings> GetCompanySettings(string companyId, short CompanySetupTypeId)
{
var filter = BaseFilter(accountId);
var projection = Builders<Company>.Projection
.Include(x => x.Id)
.Include(x => x.CompanySettings);
FindOptions<Company, CompanySettingsProjectionModel> findOptions = new FindOptions<Company, CompanySettingsProjectionModel>()
{
Projection = projection
};
var companySettings = await (await Collection.FindAsync(filter, findOptions)).FirstOrDefaultAsync();
if (companySettings != null && companySettings.CompanySettings != null && companySettings.CompanySettings.Any())
{
return companySettings.CompanySettings .FirstOrDefault(x => (int)x.CompanySetupTypeId == CompanySetupTypeId);
}
return default;
}