MongoDB 聚合错误:管道阶段规范对象必须恰好包含一个字段 [英] MongoDB Aggregation error : Pipeline stage specification object must contain exactly one field
问题描述
我是 mongodb 的新手,第一次尝试聚合.在这里,我试图获取每 15 分钟分组的推文计数.当我尝试在 mongo 控制台中运行以下查询时,出现错误:
I am new to mongodb and trying out aggregation for first time. Here, I am trying to get the count of tweets grouped by every 15 minutes. When I try to run the below query in mongo console I get the error:
管道阶段规范对象必须只包含一个字段.
A pipeline stage specification object must contain exactly one field.
db.hashtag.aggregate([
{ "$group": {
"_id": {
"year": { "$year": "$tweettime" },
"dayOfYear": { "$dayOfYear": "$tweettime" },
"interval": {
"$subtract": [
{ "$minute": "$tweettime" },
{ "$mod": [{ "$minute": "$tweettime"}, 15] }
]
}
}},
"count": { "$sum": 1 }
}
])
我在 SO 中找不到对原因的很好解释.请分享您对此主题的看法以及为什么我的查询出错.
I couldn't find a good explanation of the reason in SO. Kindly share your thoughts on this subject and why my the query has an error.
推荐答案
MongoDB 正在抱怨,因为您的管道中有一个无法识别的管道阶段规范 "count": { "$sum": 1 }
.
MongoDB is complaining because you have an unrecognised pipeline stage specification "count": { "$sum": 1 }
in your pipeline.
正确格式化后的原始管道
Your original pipeline when formatted properly
db.hashtag.aggregate([
{
"$group": {
"_id": {
"year": { "$year": "$tweettime" },
"dayOfYear": { "$dayOfYear": "$tweettime" },
"interval": {
"$subtract": [
{ "$minute": "$tweettime" },
{ "$mod": [{ "$minute": "$tweettime"}, 15] }
]
}
}
},
"count": { "$sum": 1 } /* unrecognised pipeline specification here */
}
])
should have the aggregate accumulator $sum
within the $group
pipeline as:
{
"$group": {
"_id": {
"year": { "$year": "$tweettime" },
"dayOfYear": { "$dayOfYear": "$tweettime" },
"interval": {
"$subtract": [
{ "$minute": "$tweettime" },
{ "$mod": [{ "$minute": "$tweettime"}, 15] }
]
}
},
"count": { "$sum": 1 }
}
}
])
这篇关于MongoDB 聚合错误:管道阶段规范对象必须恰好包含一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!