MongoDb aggregation $match error : “Arguments must be aggregate pipeline operators”

北城余情 提交于 2019-12-30 06:03:32

问题


I can get all stats of the site with aggregation but I want to it for a certain user, like $where.

All stats:

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

                    console.log( d )            

            })

When I try to use $match operator, I'm getting error :

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

What am I missing?


回答1:


Pipeline stages are separate BSON documents in the array:

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 )            
});

So the Array or [] bracket notation in JavaScript means it expects a "list" to be provided. This means a list of "documents" which are generally specified in JSON notation with {} braces.



来源:https://stackoverflow.com/questions/26399989/mongodb-aggregation-match-error-arguments-must-be-aggregate-pipeline-operato

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!