下面是我收集的样本记录:合并子文档主文档与MongoDB的聚集
{
_id:someId,
pages:[
{id:id,field:value},
{id:otherId,field:otherValue}
]
}
下面是我在做什么:
collection.aggregate([
{$unwind:"$pages"}
])
而其结果是类似于:
{
_id:id,
pages:{id:id,field:value}
},
{
_id:id,
pages:{id:otherId,field:otherValue}
}
不过,我想实现的是:
{
_id:id,
id:id,
field:value
},
{
_id:id,
id:otherId,
field:otherValue
}
我可以使用$project
操作将子文档合并到主文档中吗?
您列为所需输出的结果格式不正确。我不知道如何实现它。 – Tiramisu
@Tiramisu我更新了格式 – nikoss
在'$ unwind'后添加'{“$ project”:{“id”:“$ pages.id”,“field”:“$ pages.field”}}''阵列。 – styvane