Crasher Crasher - 1 month ago 10
C# Question

FilterDefinition inside an existing BsonDocument

I'm trying to change a BsonDocument with more filters as shown bellow. First I parse a string like this:

BsonDocument query = BsonDocument.Parse (configObj.Mongo_dbquery);


And then I have more filters to apply. Is there any way I can include this filter inside BsonDocument?

FilterDefinition<BsonDocument> dateFilter = Builders<BsonDocument>.Filter.Gte ("QueryDate", BsonValue.Create (startDate));


Thank you!

Answer

You could just add The BsonElements manually using the text operators

{ field : { $gt : value } }

In BsonDocument would be

var doc = new BsonDocument("field", new BsonDocument("$gt", "value"));

Then it's just a case of appending this to your main BsonDocument