作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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 驱动程序 1.9.2。谢谢。
最佳答案
首先,添加一个构建器:
var builder = Builders<BsonDocument>.Filter;
然后是这样的过滤器:
var filter = builder.Lt("quantity", 20) | builder.Eq("price", 10) | other stuff)
最后是这样的:
db.collection.Find(filter).ToList();
关于c# - 如何在 MongoDB C# 驱动程序中使用 "Or"语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39573518/
我是一名优秀的程序员,十分优秀!