【问题标题】:MongoDb aggregation $match error : "Arguments must be aggregate pipeline operators"MongoDb 聚合 $match 错误:“参数必须是聚合管道运算符”
【发布时间】:2014-12-11 13:25:52
【问题描述】:

我可以使用 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

我错过了什么?

【问题讨论】:

    标签: javascript node.js mongodb mongodb-query aggregation-framework


    【解决方案1】:

    管道阶段是数组中单独的 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 表示法指定,带有 {} 大括号。

    【讨论】:

    • 谢谢。但是当我查询用户时,我只得到[]。 (当我删除 $match 的内容时,它正在工作(提供所有统计信息)。)
    • 我刚刚从$game_user_id 中删除了$ 并且它起作用了,我永远不会理解mongodb .. 无论如何再次感谢。
    【解决方案2】:
     const stats = await Tour.aggregate([
     
        {
            $match: { ratingsAvarage: { $gte: 4.5 } }
        }, 
        {    
            $group: { 
            _id: null,
            avgRating: { $avg: '$ratingsAvarage' }
                       
             }
        }]
    

    确保在数组中使用 as obj 或 {} 分隔管道阶段。所以 $match 和 $group 必须在一个对象内。

    【讨论】:

      【解决方案3】:

      另一个可能的原因是使用的 mongodb 版本和 mongoose 版本不兼容。

      就我而言

      mongodb 版本: MongoDB shell版本v3.6.18

      猫鼬版: "猫鼬": "^5.1.3",

      请查找兼容版本列表

      https://mongoosejs.com/docs/compatibility.html

      【讨论】:

        【解决方案4】:

        另一个可能的原因可能是管道内的空对象 {}。如果您在管道中使用任何条件阶段,就会发生这种情况。

        例如:

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

        这会导致同样的错误“Arguments must be aggregate pipeline 运营商”

        相反,您可以使用以下内容:

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

        【讨论】:

          猜你喜欢
          • 2019-03-27
          • 2016-09-17
          • 2018-04-24
          • 2021-06-12
          • 2017-08-24
          • 1970-01-01
          • 2021-06-16
          • 2020-09-10
          • 2018-04-22
          相关资源
          最近更新 更多