MongoDB C#Driver Projection的字段

时间:2016-08-11 09:59:11

标签: c# mongodb database nosql

您好我有一个集合在mongoDB中,我想从它只获取部分字段,我创建了一个我正在向Mongo插入数据的类

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);

**现在我使用此代码从集合中获取所有字段,但我想将Frame字段退出类,我试图构建不同的类没有此字段但我不知道如何不收到框架字段**

                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();

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

请看这个:

   _result = _collection.Find(o => o._id == _id)
            .Project<FrameDocumentNoFrameField>  
          (Builders<FrameDocument>.Projection.Exclude(f => f.Frame)).ToList();

其中FrameDocumentNoFrameField是没有Frame字段的类

source here

答案 1 :(得分:1)

#示例:模型类

select * from fb_lab_test 
where (report_item_code = 'HBcAb') 
or (
    report_item_code = 'Anti-Hbc' and 
    case isnumeric(result) when 1 then try_cast(result as float) else 10000.0 end > 0.2
)

##现在创建要为其读取值的Projection类

    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; }
    }

#创建投影后,使用Builders从mongo获取数据

[BsonIgnoreExtraElements]
    public class CompanySettingsProjectionModel
    {  
        public List<CompanySettings> CompanySettings { get; set; }
    }