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

39,970

Solution 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.

Solution 2

Another possible reason could be due to an empty object {} inside the pipeline. It can happen if you are using any conditional stages in the pipeline.

eg:

const pipeline = []:
.....
.....
let sort = {}
if(params.sort){
 sort = { $sort: { popularity: -1 } }
}

pipeline.push(sort)

This will cause the same error "Arguments must be aggregate pipeline operators"

Instead you can use something like this:

let sort;
if(params.sort){
 sort = { $sort: { popularity: -1 } }
}
    
pipeline.concat(sort? [sort] : [])

Solution 3

 const stats = await Tour.aggregate([
 
    {
        $match: { ratingsAvarage: { $gte: 4.5 } }
    }, 
    {    
        $group: { 
        _id: null,
        avgRating: { $avg: '$ratingsAvarage' }
                   
         }
    }]

Make sure to separate the pipeline stages with as obj or {} inside the array. So $match and $group have to be inside an object.

Share:
39,970

Related videos on Youtube

Lazy
Author by

Lazy

hi.

Updated on December 01, 2021

Comments

  • Lazy
    Lazy over 2 years

    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?

  • Lazy
    Lazy over 9 years
    I thank you. But when I queried for a user, I'm getting only []. (When I delete content of $match it's working (giving all stats).)
  • Lazy
    Lazy over 9 years
    I just deleted the $ from $game_user_id and it worked, I will never understand mongodb.. Anyway thanks again.