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