gpt4 book ai didi

mongodb - 综合平均值

转载 作者:行者123 更新时间:2023-12-04 00:36:51 25 4
gpt4 key购买 nike

我希望在 MongoDB 聚合中找到的平均值得出一个综合值

 [
{
"_id": "581c3d9c55421c16ecaf0b26",
"uDept": "CoE Design & Research",
"uName": "Saurabh Raman",
"rateVal": 3,
"timeStamp": "11/4/2016 13:17",
"__v": 0
},
{
"_id": "581c437ea3d9f6087ce3b21e",
"uDept": "CoE Design & Research",
"uName": "Sachin P Singh",
"rateVal": 4,
"timeStamp": "11/4/2016 13:42",
"__v": 0
},
{
"_id": "581c6d74a3d9f6087ce3b21f",
"uDept": "Training",
"uName": "Rahul Shakya",
"rateVal": 4,
"timeStamp": "11/4/2016 16:35",
"__v": 0
}
]

我运行以获取“rateVal”平均值的查询
db.calcR.aggregate([
{
$group: {
_id: null,

avg: {$avg: '$rateVal'},

}
}

输出为 { avg: 3.6666 }但理想情况下我希望它显示为 4
[
{
"_id": null,
"avg": 3.6666666666666665,

}
]

最佳答案

要舍入 mongoDb 中的值,您可以使用 $ceil$floor

db.calcR.aggregate([
{
$group: {
_id: null,

avg: {$avg: '$rateVal'},

},
$project:{
_id: '$_id' ,
roundedAvg : { $ceil: '$avg' }
}
}]

输出看起来像
[
{
"_id": null,
"roundedAvg": 4,

}
]

关于mongodb - 综合平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40459844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com