I am trying to create a query that finds all big, red things with a value greater than 3.
This query seems to be what I need:
{ "color" : "red", "size" : "large", "cost" : { "$gt" : 3.0 } }
But I can’t find an elegant way to create a cost condition using the official MongoDB CSharp driver. This is one hack that seems to create a request:
QueryConditionList gt = Query.GT("cost", BsonDouble.Create(3));
QueryDocument query = new QueryDocument();
query.Add("color", "red");
query.Add("size", "large");
query.Add(gt.ToBsonDocument().Elements);
List<BsonDocument> results = events.Find(query).ToList();
Another way to do this, which works, is as follows:
QueryDocument query = new QueryDocument();
query.Add("color", "red");
query.Add("size", "large");
query.Add("cost", new BsonDocument("$gt", BsonDouble.Create(3)));
List<BsonDocument> results = events.Find(query).ToList();
Is any of these approaches a good way to achieve this? Is there some more?
I need to use methods that allow me to dynamically build a query and add fields that will be involved in the query. I was hoping to find a way to add a condition through query.Add (), but I don't know if this is possible.
Any help is appreciated.