需要帮助才能汇总$ group状态
我收集了1000个文件的集合:
{
"_id" : ObjectId("628b63d66a5951db6bb79905"),
"index" : 0,
"name" : "Aurelia Gonzales",
"isActive" : false,
"registered" : ISODate("2015-02-11T04:22:39.000+0000"),
"age" : 41,
"gender" : "female",
"eyeColor" : "green",
"favoriteFruit" : "banana",
"company" : {
"title" : "YURTURE",
"email" : "[email protected]",
"phone" : "+1 (940) 501-3963",
"location" : {
"country" : "USA",
"address" : "694 Hewes Street"
}
},
"tags" : [
"enim",
"id",
"velit",
"ad",
"consequat"
]
}
我想按年份和性别进行分组。像2014年男性注册105和女性注册131。最后返回这样的文件:
{
_id:2014,
male:105,
female:131,
total:236
},
{
_id:2015,
male:136,
female:128,
total:264
}
我已经尝试了注册
和性别
this:this:this:
db.persons.aggregate([
{ $group: { _id: { year: { $year: "$registered" }, gender: "$gender" }, total: { $sum: NumberInt(1) } } },
{ $sort: { "_id.year": 1,"_id.gender":1 } }
])
它是这样的返回文档:
{
"_id" : {
"year" : 2014,
"gender" : "female"
},
"total" : 131
}
{
"_id" : {
"year" : 2014,
"gender" : "male"
},
"total" : 105
}
请指导从整体中弄清楚。
I have a collection of 1000 documents like this:
{
"_id" : ObjectId("628b63d66a5951db6bb79905"),
"index" : 0,
"name" : "Aurelia Gonzales",
"isActive" : false,
"registered" : ISODate("2015-02-11T04:22:39.000+0000"),
"age" : 41,
"gender" : "female",
"eyeColor" : "green",
"favoriteFruit" : "banana",
"company" : {
"title" : "YURTURE",
"email" : "[email protected]",
"phone" : "+1 (940) 501-3963",
"location" : {
"country" : "USA",
"address" : "694 Hewes Street"
}
},
"tags" : [
"enim",
"id",
"velit",
"ad",
"consequat"
]
}
I want to group those by year and gender. Like In 2014 male registration 105 and female registration 131. And finally return documents like this:
{
_id:2014,
male:105,
female:131,
total:236
},
{
_id:2015,
male:136,
female:128,
total:264
}
I have tried till group by registered
and gender
like this:
db.persons.aggregate([
{ $group: { _id: { year: { $year: "$registered" }, gender: "$gender" }, total: { $sum: NumberInt(1) } } },
{ $sort: { "_id.year": 1,"_id.gender":1 } }
])
which is return document like this:
{
"_id" : {
"year" : 2014,
"gender" : "female"
},
"total" : 131
}
{
"_id" : {
"year" : 2014,
"gender" : "male"
},
"total" : 105
}
Please guide to figure out from this whole.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
play
Play
只需在您的聚合管道中再添加一个小组阶段,例如:
这是工作 link 。我们在最后一步中按一年进行分组,并计算有条件的性别计数,而总数只是计数的总数,而不论性别如何。
Just add one more group stage to your aggregation pipeline, like this:
Here's the working link. We are grouping by year in this last step, and calculating the counts for gender conditionally and the total is just the total of the counts irrespective of the gender.
Besides @Gibbs mentioned in the comment which proposes the solution with 2
$group
stages,You can achieve the result as below:
$group
- Group by year of <代码>注册。将性别
值添加到genders
array。$ sort
- 订购_id
。$ project
- 装饰输出文档。3.1。
男性
- 从$ filter
获取数组的大小“男性”在“ genders”数组中的值。3.2。
女性
- 从$ filter
获取数组的大小“女性”阵列中的“女性”数量。3.3。
Total
- 获取“ Genders”数组的大小。如果您期望您计算并返回“男性”和“女性”性别领域,请提出此方法。
示例mongo playground
Besides @Gibbs mentioned in the comment which proposes the solution with 2
$group
stages,You can achieve the result as below:
$group
- Group by year ofregistered
. Addgender
value intogenders
array.$sort
- Order by_id
.$project
- Decorate output documents.3.1.
male
- Get the size of array from$filter
the value of "male" in "genders" array.3.2.
female
- Get the size of array from$filter
the value of "female" in "genders" array.3.3.
total
- Get the size of "genders" array.Propose this method if you are expected to count and return the "male" and "female" gender fields.
Sample Mongo Playground