【发布时间】:2022-01-03 06:46:19
【问题描述】:
我正在寻找价格高于平均价格的产品。
我知道如何取平均值:
db.products.aggregate([{
"$group": {
"_id": null,
"average": { "$avg": "$price" }
}
},
{ $project : { _id : 0 } } ])
但是如何在 $gt 子句中使用它呢?
例如,我尝试将结果保存在变量中:
var averageValue =
db.products.aggregate([{
"$group": {
"_id": null,
"average": { "$avg": "$price" }
}
},
{ $project : { _id : 0 } } ])
然后在$gt子句中使用:
db.products.find({ "price": { "$gt": averageValue} })
但是,它似乎没有打印任何东西。
我也想知道这是否可以在单个查询中完成。
【问题讨论】:
标签: mongodb nosql aggregation-framework