mongodb 字段“$name”必须是累加器对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54440636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:01:14  来源:igfitidea点击:

The field "$name" must be an accumulator object

mongodbmongoosemongodb-queryaggregation-framework

提问by Matheus Barem

I have a query and when I use a $groupa error shows "the field "$name must be an accumulator object", if if remove the filed "$name" all works well and i have tried to use only "name" instead of "$name" and the error continues.

我有一个查询,当我使用$group 时,错误显示“字段“$name 必须是累加器对象”,如果删除提交的“$name”一切正常并且我尝试仅使用“name” "$name" 并且错误继续。

   User.aggregate([
    {
      $match: {
        "storeKey": req.body.store        
      }
  },
  {
      $group: {
          "_id": "$_id",          
          "name": "$name",              
          "count": {
              "$sum": 1
          },
          "totalValue": {
              "$sum": "$value"
          }      
      }
  },
  {
    $sort: sort
  },
  {
     $skip: req.body.limit * req.body.page
  },
  {
     $limit: req.body.limit
  }
])...

回答by Ashh

There are some aggregation operators that can only be used in $groupaggregation and named as $group accumulators

有一些聚合运算符只能在$group聚合中使用并命名为$group accumulators

Just as you used $sumhere you have to use for the namekey as well

就像您$sum在这里使用的一样,您也必须将其用于name密钥

{ "$group": {
  "_id": "$_id",
  "name": { "$first": "$name" },  //$first accumulator
  "count": { "$sum": 1 },  //$sum accumulator
  "totalValue": { "$sum": "$value" }  //$sum accumulator
}}

Accumulator is like array of Elements its Accumulates as Array. $first -> gives 1st name that goes in the group of names

累加器就像元素数组,它以数组形式累加。$first -> 给出名称组中的第一个名称

Example: so if you have $_idsame but different name ["Darik","John"]specifying $firstwill give Darik & similarly $lastwill give John

示例:因此,如果您具有$_id相同但不同的名称,则["Darik","John"]指定$first将给 Darik,同样$last将给 John

回答by Manish Solanki

db.faq_feedback.aggregate({ 
   $lookup:{ 
      "from":"faq",
      "localField":"question_id",
      "foreignField":"_id",
      "as":"faq"
   }
},
{ 
   $unwind:"$faq"
},

{ 
   $project:{ 
      "question_id":1,
      "lang":"$faq.lang",
      "feedback":"$faq.feedback",
      "question":"$faq.question",
      "yes":{ 
         "$cond":[ 
            { 
               "$eq":[ 
                  "$feedback",
                  "yes"
               ]
            },
            1,
            0
         ]
      },
      "no":{ 
         "$cond":[ 
            { 
               "$eq":[ 
                  "$feedback",
                  "no"
               ]
            },
            1,
            0
         ]
      }
   }
},

{ 
   $group:{ 
      "_id":"$question_id",

      "yes":{ 
         "$sum":"$yes"
      },
      "no":{ 
         "$sum":"$no"
      },
      "question":{"$first":"$question"},
      "lang":{"$first":"$lang"}
   }
},
{ 
   $limit:10000
},
{ 
   $skip:0
})

回答by Mohit Kumar Bordia

$group: {
    _id:"$_id", 
    "name" :{ $last: '$name' }, 
    "count" : { $sum: 1 }, 
    "totalValue": { "$sum": "$value" }
}