Reputation: 926
I have developed a simple API which allows you to build up an array of search criteria within a MongoDB Collection. I now need to be able to convert this array into an actual Mongo Query, and this part is where I am having extreme difficulty.
Ideally I am after some syntax that will allow me to do the following pseudo code:
var query = new QueryBuilder();
foreach (var group in groups)
{
switch (group.Condition)
{
case GroupCondition.Or:
query.Or(group.Queries);
break;
case GroupCondition.And:
query.And(group.Queries);
break;
}
}
return myCollection.FindAs(type, query);
I actually want to build up slightly more complex queries, but ultimately I am after the functionality to dynamically build up my queries with objects as seen in my pseudo code above.
Feel free to ask me for additional details if I have not made myself clear enough about what I am trying to achieve.
Upvotes: 9
Views: 3359
Reputation: 12624
Seems like you have the right idea... There is a class called Query that is essentially a query builder without the instantiation.
using MongoDB.Driver.Builders;
Query.And, Query.Or, etc... are all there. It is the same thing that is used underneath the linq provider to build up complex queries.
Upvotes: 4