5

I need to query MongoDB with a standard query like the following:

{"$and":[{"Name":"Accelero JKT M Tun XL "}]}

I typically build queries using the Query object in C# and then do something like this:

MongoCollection<BsonDocument> col = _database.GetCollection<BsonDocument>("SourceItem");
var query = Query.And(Query.EQ("AccountID", BsonValue.Create(Convert.ToInt32(_accountID))), Query.EQ("SKU", sku));
var docs = col.Find(query);

Since I already have the query, I do not want to use the Query object to build a query. How do I simply use the query that I already have?

Evil August
  • 411
  • 6
  • 18
  • 2
    Have you looked at this? http://stackoverflow.com/questions/6120629/can-i-do-a-text-query-with-the-mongodb-c-sharp-driver – Matt Oct 22 '14 at 15:51

1 Answers1

5

There is a slightly simpler way to do this (you should just replace " with '):

var el = BsonDocument.Parse("{'$and':[{'Name':'Accelero JKT M Tun XL '}]}");
var doc = new QueryDocument(el);
var result = coll.Find(doc);
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
Disposer
  • 6,201
  • 4
  • 31
  • 38