Sunday, November 17, 2019

Excluding fields in MongoDB using Asp.net



using MongoDB.Bson;
using MongoDB.Driver;                             
          
static MongoClient client = new MongoClient("mongodb://localhost:27017"); //Set the connection string

static IMongoDatabase db = client.GetDatabase("test"); //Database name

public void Exclude()
{
   IMongoCollection<BsonDocument> collection = db.GetCollection<BsonDocument>("user");

   var filter = Builders<BsonDocument>.Filter.Empty;   

 // uncomment the below two lines to exclude the required things

// var project = Builders<BsonDocument>.Projection.Exclude("_id"); //replace the field which you want to exclude in place of _id and it must match the field name in the MongoDB document

//var res = collection.Find<BsonDocument>(filter).Project(project).ToList();

    var res = collection.Find(filter).ToList(); //Comment this after un commenting the above two line

}

Before excluding _id field

After 



No comments:

Post a Comment

Git Commands

Git Version   To check the git version Git -v       Git Clone To clone the repository, use the following command: Git clone [u...