2016-09-19 78 views
0

我在MongoDB C#中查询以下内容时遇到问题。我在mongo客户端的代码是如何在MongoDB C#驱动中使用“Or”语句?

db.collection.find({ $or: [ { quantity: { $lt: 20 } }, { price: 10 } ,{price:100},{name:"x"}] }) 

但是如何在C#中查询相同的内容。 我能够查询以下蒙戈客户端代码语句

db.collection.find({type:"food"},{name:1,quantity:1}) 

var match = new BsonDocument() { { "$match", new BsonDocument { {"type":"food" } } } }; 
var project = new BsonDocument(){ { "$project", new BsonDocument{ { "name", 1 } { "quantity", 1 } } } }; 
AggregateArgs AggregationPipeline = new AggregateArgs() { Pipeline = new[] { match, project } }; 
var aggregate = Collection.Aggregate(AggregationPipeline); 

我使用蒙戈升C驱动器1.9.2。 谢谢。

回答

1

首先,添加一个建设者:

var builder = Builders<BsonDocument>.Filter; 

然后过滤这样的:

var filter = builder.Lt("quantity", 20) | builder.Eq("price", 10) | other stuff) 

最后是这样的:

db.collection.Find(filter).ToList(); 
+0

嗨@Mahdi,我使用MongoCSharpDriver。 1.9.2。此版本中不存在构建器上下文。谢谢 – coder