Search code examples
c#.netmongodbmongodb-.net-drivermongodb-csharp-2.0

MongoDb c# Driver typed Builder with string args


How can I implement a typed Builder using string fieldname and value, like in the following code:

Builders<Profile>.Filter.Eq(fieldName, value)

I can implement it with BsonDocument data type, but I need to retrieve a typed Profile FilterDifinition.


Solution

  • There's an implicit conversion from string to FilterDefinition so you can use it just like you did. For example:

    var profile = await collection.Find(Builders<Profile>.Filter.Eq("Name", "bar")).SingleAsync();