Search code examples
c#mongodb-.net-driver.net-7.0

Assignment of BsonDocument to FilterDefinition<TDocument>


Asking this question out of curiosity:

I'm exploring MongoDB when I stumble upon the following

var client = new MongoClient("mongodb://localhost:27017/");
var collection = db.GetCollection<Foo>("some_collection");

// IMongoCollection.Find() accepting a 'BsonDocument' instance as input parameter.
var documents = collection.Find(new BsonDocument()).ToList();

The codes compiles and runs just fine, but I can't get my head around what comes next.

BsonDocument derived from BsonValue, but there's doesn't seems to have any overload of IMongoCollection.Find() that can accept BsonDocument as input parameter.

How does IMongoCollection.Find() being able to accept a BsonDocument instance as input parameter?


Solution

  • It uses implicit operator to convert a set of types (BsonDocument, string and etc) to FilterDefinition. You can find some details here