如何使用“或”语句在MongoDB c#驱动程序

本文关键字:MongoDB 驱动程序 语句 何使用 | 更新日期: 2023-09-27 18:07:45

我在MongoDB c#中查询以下内容时面临问题。我在mongo客户端的代码是

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

,但如何查询相同的c#。我能够查询以下mongo客户机代码语句

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);

我使用Mongo C Sharp Driver 1.9.2。谢谢。

如何使用“或”语句在MongoDB c#驱动程序

首先,添加一个构建器:

var builder = Builders<BsonDocument>.Filter;

然后是这样的过滤器:

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

最后是:

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