数据如下:
[{_id:1,price:"5"},{_id:2,price:"10"},{_id:3,price: null}]
预期结果是 price 字段中所有值的平均值。平均5和10,即7.5
答案 0 :(得分:0)
您应该能够以平均(https://docs.mongodb.com/manual/reference/operator/aggregation/avg/)进行分组
db.collection.aggregate([
{
$group: {
_id: "",
price: {
$avg: "$price"
}
}
}
])
执行时将输出
[
{
"_id": "",
"price": 7.5
}
]