字段"$ name"位于必须是一个累加器对象 [英] The field "$name" must be an accumulator object
问题描述
我有一个查询,当我使用 $ group 错误显示字段"$ name必须是累加器对象"",如果删除归档的"$ name",则一切正常,并且我尝试仅使用"name"而不是"$ name",并且错误继续.
I have a query and when I use a $group a 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.
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
}
])...
推荐答案
有些聚合运算符只能在 $group accumulators
There are some aggregation operators that can only be used in $group
aggregation and named as $group accumulators
就像您使用> $sum
在这里,您还必须使用name
键
Just as you used $sum
here you have to use for the name
key as well
{ "$group": {
"_id": "$_id",
"name": { "$first": "$name" }, //$first accumulator
"count": { "$sum": 1 }, //$sum accumulator
"totalValue": { "$sum": "$value" } //$sum accumulator
}}
累加器就像元素数组一样,其累加为数组. $ first->给出名称组中第一个名称
Accumulator is like array of Elements its Accumulates as Array. $first -> gives 1st name that goes in the group of names
示例:
因此,如果您具有$_id
相同但不同的名称["Darik","John"]
指定$first
将为Darik&类似地,$last
将给约翰
Example:
so if you have $_id
same but different name ["Darik","John"]
specifying $first
will give Darik & similarly $last
will give John
这篇关于字段"$ name"位于必须是一个累加器对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!