C# Driver: Dynamic query

1,510 views
Skip to first unread message

Perry

unread,
Feb 8, 2012, 1:11:15 PM2/8/12
to mongodb-user
I have been working on a method that accepts some optional parameters,
then builds a query based on those optional parameters to query my
collection. What I have is working but I am looking for a better way
if it exists.

BsonDocument bd = new BsonDocument();
if(!String.IsNullOrEmpty(agency))
{
bd.Add(Query.EQ("Agency",agency).ToBsonDocument());
}
if(categories != null && categories.Length > 0)
{
BsonArray ba = new BsonArray(categories);
bd.Add(Query.In("Categories",ba).ToBsonDocument());
}

QueryDocument qd = new QueryDocument(bd);
var unpublished = _unpublishedNewsArticles.FindAs<NewsArticle>(qd);

Essentially, I just have some conditional statements that add to my
BsonDoc, then I stuff my BsonDoc into a QueryDoc and use that to query
my collection. This works but feels like a hack. Essentially what I
want to do is a Query.And(q1, q2) and add the different queries
conditionally. Is there a better way to do this with the C# driver?

Robert Stam

unread,
Feb 8, 2012, 1:41:44 PM2/8/12
to mongod...@googlegroups.com
Part of the perceived awkwardness probably comes from mixing models. The C# driver lets you work with queries either at a low level with QueryDocument (similar to writing a query in JSON in the mongo shell) or at a higher level using the Query builder. When building dynamic queries you are probably better off working exclusively at one level, either like this:

    var query = new QueryDocument();
    if (!string.IsNullOrEmpty(agency))
    {
        query.Add("Agency", agency);
    }
    if (categories != null && categories.Count != 0)
    {
        query.Add("Categories", new BsonDocument("$in", categories));
    }

Or if you wanted to do something similar entirely at a slightly higher level using the Query builder you could write this:

    IMongoQuery query = new QueryDocument();
    if (!string.IsNullOrEmpty(agency))
    {
        query = Query.And(query, Query.EQ("Agency", agency));
    }
    if (categories != null && categories.Count != 0)
    {
        query = Query.And(query, Query.In("Categories", categories));
    }

In both cases you'd want to think about what it means if you end up with an empty query (if none of the if statements match).


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Perry

unread,
Feb 8, 2012, 2:00:00 PM2/8/12
to mongodb-user
Thanks Robert, that answers my question. In this case the empty query
is ok, as the optional parameters are just acting as filters. If you
don't provide any filters, you get back all the results, which are
paged (I didn't include that part of the code).
Reply all
Reply to author
Forward
0 new messages