gpt4 book ai didi

javascript - MongoDb 聚合 $match 错误 : "Arguments must be aggregate pipeline operators"

转载 作者:IT老高 更新时间:2023-10-28 13:14:48 25 4
gpt4 key购买 nike

我可以使用 aggregation 获取站点的所有统计信息,但我想为特定用户获取它,例如 $where

所有统计数据:

games.aggregate([{
$group: {
_id: '$id',
game_total: { $sum: '$game_amount'},
game_total_profit: { $sum: '$game_profit'}}
}]).exec(function ( e, d ) {

console.log( d )

})

当我尝试使用 $match 运算符时,出现错误:

games.aggregate([{
$match: { '$game_user_id' : '12345789' },
$group: {
_id: '$id',
game_total: { $sum: '$game_amount'},
game_total_profit: { $sum: '$game_profit'}}
}]).exec(function ( e, d ) {

console.log( d )

})

Arguments must be aggregate pipeline operators

我错过了什么?

最佳答案

管道阶段是数组中单独的 BSON 文档:

games.aggregate([
{ $match: { 'game_user_id' : '12345789' } },
{ $group: {
_id: '$id',
game_total: { $sum: '$game_amount'},
game_total_profit: { $sum: '$game_profit'}}
}}
]).exec(function ( e, d ) {
console.log( d )
});

因此,JavaScript 中的 Array 或 [] 括号表示法意味着它期望提供一个“列表”。这意味着一个“文档”列表,这些“文档”通常以 JSON 表示法指定,带有 {} 大括号。

关于javascript - MongoDb 聚合 $match 错误 : "Arguments must be aggregate pipeline operators",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26399989/

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