Sorry, my English is not very good.I try to express my thoughts as clearly as possible,I have a code example
{name:"Danny", age:20, gender: 0}
{name:"Jordan", age:15, gender: 1}
{name:"Li Mei", age:20, gender: 1}
{name:"Soojin", age:17, gender: 0}
{name:"Zhang Ming", age:22, gender: 1}
I want to get the following results by grouping according to gender
{
"Test field":[{name:"Danny", age: 20}, {name:"Soojin", age: 17}]
}
{
"Test field":[{name:"Jordan", age: 15}, {name:"Li Mei", age: 20}, {name: "Zhang Ming", age: 22}]
}
Do you have some ideas ?
Thanks in advance
Best regards
CodePudding user response:
Try this:
db.collection.aggregate([
{
"$group": {
"_id": "$gender",
"Test Field": {
"$push": {
age: "$age",
name: "$name"
}
}
}
},
{
"$project": {
_id: 0
}
}
])
See it working here.