如何使用C#驱动程序以字符串格式查询MongoDB?

时间:2021-04-14 03:03:21

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

我需要使用如下标准查询来查询MongoDB:

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

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

我通常使用C#中的Query对象构建查询,然后执行以下操作:

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?

由于我已经有了查询,因此我不想使用Query对象来构建查询。我如何简单地使用我已有的查询?

1 个解决方案

#1


5  

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

有一种稍微简单的方法(你只需要替换“with”):

var el = BsonDocument.Parse("{'$and':[{'Name':'Accelero JKT M Tun XL '}]}");
var doc = new QueryDocument(el);
var result = coll.Find(doc);

#1


5  

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

有一种稍微简单的方法(你只需要替换“with”):

var el = BsonDocument.Parse("{'$and':[{'Name':'Accelero JKT M Tun XL '}]}");
var doc = new QueryDocument(el);
var result = coll.Find(doc);