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

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of MongoDb aggregation $match error : “Arguments must be aggregate pipeline operators” without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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?

Answer

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.

We are here to answer your question about MongoDb aggregation $match error : “Arguments must be aggregate pipeline operators” - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji