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

时间:2019-01-30 12:26:01

标签: mongodb mongoose aggregation-framework

我有一个查询,当我使用$group时,错误显示“字段“ $ 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
  }
])...

3 个答案:

答案 0 :(得分:1)

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

就像在这里使用$sum一样,您也必须同时使用name

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

答案 1 :(得分:0)

    String outlet = request.getParameter("outlet");
    String CategoryCode= request.getParameter("categoryCode");
   System.out.println("outlet in new file :"+outlet);

答案 2 :(得分:-1)

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