下面是我收集的樣本記錄:合併子文檔主文檔與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