我在 mongodb 中有以下集合,我们称之为“产品”:
[
{
"name" : "Product X",
"warehouseStock" : 50,
"reservedStock" : 41
},
{
"name" : "Product Y",
"warehouseStock" : 50,
"reservedStock" : 10
}
]
我想要一个 find() 查询,该查询返回此集合中“warehouseStock”小于(“reservedStock”+阈值)的文档,其中阈值是传递到 find() 查询中的参数。
我正在尝试在 Node 中执行以下 find() 查询:
var threshold = 10;
mongo.getDb().collection('products').find({warehouseStock: {$lt:(threshold+'$reservedStock')}})
但这似乎不起作用。我知道我可以执行以下操作:
mongo.getDb().collection('products').find({warehouseStock: {$lt:threshold}})
但是我如何查询位置
warehouseStock < (reservedStock + threshold)
因此,如果阈值为 =10,那么我只会获得上面集合示例中两项中的第一项。
谢谢!
最佳答案
您可以使用$where
:
var threshold = 10;
mongo.getDb().collection('products').find({
"$where": "this.warehouseStock < (this.reservedStock + " + threshold + ")"
}).toArray(function(err, items) {
console.log(items);
});
关于node.js - mongodb查找低于另一个字段的字段加上参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43306657/