Appearance
原测试数据:
[
{ "name": "jack", "age": 16, "sex": "male" },
{ "name": "lucy", "age": 16, "sex": "female" },
{ "name": "lili", "age": 18, "sex": "female" },
{ "name": "jame", "age": 17, "sex": "unknown" }
]
以 age 聚合,把 name 输出为数组
db.a_test.aggregate([
{
$group: {
_id: '$age',
name: {
'$push': '$name'
}
}
}
]);
获取到的数据
[
{ "age": 16, "name": ["jack", "lucy"] },
{ "age": 17, "name": ["jame"] },
{ "age": 18, "name": ["lili"] },
]