我的当前聚合输出如下:
[
{
"courseCount": 14
},
{
"registeredStudentsCount": 1
}
]
该数组有两个文档.我想将所有文档合并为一个文档,其中包含mongoDB中的所有字段
我的当前聚合输出如下:
[
{
"courseCount": 14
},
{
"registeredStudentsCount": 1
}
]
该数组有两个文档.我想将所有文档合并为一个文档,其中包含mongoDB中的所有字段
db.collection.aggregate([
{
$group: {
_id: 0,
merged: {
$push: "$$ROOT"
}
}
},
{
$replaceRoot: {
newRoot: {
"$mergeObjects": "$merged"
}
}
}
])
解释: