To find one item from mongo collection, I am trying to apply filter and to the collection. But there is a compilation error as below.
This code is taken from official mongodb
docs
var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
var result = _collection.Find(filter);
Generic Type of Builder
should be the same as for collection's generic type. In your case collection should have type BsonDocument.
var _collection = database.GetCollection<BsonDocument>("name");
var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
var result = _collection.Find(filter);
When the "_id" is a BsonType.ObjectId, you will need to use ObjectId.Parse like this:
var _collection = database.GetCollection<BsonDocument>("name");
var filter = Builders<BsonDocument>.Filter.Eq("_id", ObjectId.Parse(id));
var result = _collection.Find(filter);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With