Kar*_*son 2 mongodb mongodb-query mongoskin aggregation-framework
我有以下文件:
{
array: [
{
type: 'error',
data: 1
},
{
type: 'error',
data: 2
}
]
}
Run Code Online (Sandbox Code Playgroud)
无论如何,我是否只能获取每个数组元素中的数据字段并将其作为数组返回?就像下面这样:
[1, 2] // <--- array containing only the data fields
Run Code Online (Sandbox Code Playgroud)
mongodb 投影文档似乎没有涵盖这一点?
您可以使用聚合和$map
运算符。
db.collection.aggregate([
{
"$project": { "_id": 0, "data": {
"$map": { "input": "$array", "as": "ar", "in": "$$ar.data" } } }
}
])
Run Code Online (Sandbox Code Playgroud)
其产量:
{ "data" : [ 1, 2 ] }
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7688 次 |
最近记录: |