MongoDB 聚合断言:命令失败

MongoDB aggregate assert: command failed

我使用此查询来查找状态("isActive": false)true or false 还根据状态最后计数总金额查找年龄

db.programmershelper.aggregate([{
    $match: {
        "isActive": false
    }
}, {
    $group: {
        _id: "age",
        total: { $count: "$amount" } 
    }
}])

同时显示此消息 断言:命令失败:{ "ok" : 0, "errmsg" : "unknown group operator '$count'", "code" : 15952, "codeName" : "Location15952" }

跟随错误

aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13

您可以按照 groupcount, sum and average docs 找到符合您要求的确切解决方案。

至于我从问题中了解到的内容,您想根据过滤器 "isActive": false 进行汇总,并根据最后的总计数查找年龄。

db.programmershelper.aggregate([{
    $match: {
        "isActive": false
    }
}, {
    $group: {
        _id: "age",
        count: { $sum: 1 }
    }
}])