MongoDB C# Driver Projection of fields

11,137

Solution 1

please see this:

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

where FrameDocumentNoFrameField is a class without Frame field

source here

Solution 2

#Example: Model class

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

#Now create a Projection class for which you want to read values

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

#After Creating projection,fetch data from mongo using Builders

     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;
        }
Share:
11,137
Shimon Wiener
Author by

Shimon Wiener

CEO of small software company, working on all kind of projects and technologies including Microsoft in all levels and Apple mobile devices. We specialized in: SQL Server Technologies. Microsoft WEB Technologies. IOS Native Applications. jQuery Mobile jQuery. PhoneGap/Cordova. WPF/Silverlight. Sencha Touch. EXT-JS Microsoft Server Technologies. Android Native Applications

Updated on June 05, 2022

Comments

  • Shimon Wiener
    Shimon Wiener almost 2 years

    Hi i have a collection In mongoDB that i want to get only part of the fields from it, i created a class that i'm inserting data with to 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; }
    
    
    }
    

    This is the insert code:

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

    **For now i get all The fields from the collection with this code, But i want to leave the Frame field out of the class, i tried to build different class without this field but i don't know how to not receive the Frame field **

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

    Can anyone help?