我有这样的文件收藏:
{
"_id": 1,
"people": [
{
"name": "Bob",
"age": "15"
},
{
"name": "Alice",
"age": "18"
}
]
}
我的查询是:
db.groups.aggregate({ $match: { "_id": 1 }}, { $project: { "_id": 0, "people.name": 1 } })
此查询返回:
{
"people": [
{
"name": "Bob"
},
{
"name": "Alice"
}
]
}
但是我需要这样的结果:
{ "names": [ "Bob", "Alice" ] }
我应该将哪些参数添加到.aggregate()函数中?
答案 0 :(得分:0)
解决方案是:
db.groups.aggregate({ $match: { "_id": 1 }}, { $project: { "_id": 0, "names": "$people.name" } })