我有以下檔案:
{
"_id": ObjectId("10..."),
"ownerName": "Merchant 10",
...,
"stores": [
{
"id": ObjectId("20..."),
"storeName": "Store 20"
},
{
"id": ObjectId("21..."),
"storeName": "Store 21"
}
]
},
{
"_id": ObjectId("11..."),
"ownerName": "Merchant 11",
...,
"stores": [
{
"id": ObjectId("22..."),
"storeName": "Store 22"
}
]
}
聚合是否可以基于嵌套陣列“商店”回傳分頁結果,并最終得到以下結果:
{
"_id": "20...",
"ownerName": "Merchant 10",
"storeName": "Store 20"
}, {
"_id": "21...",
"ownerName": "Merchant 10",
"storeName": "Store 21"
}, {
"_id": "22...",
"ownerName": "Merchant 11",
"storeName": "Store 22"
},
所以基本上,我希望能夠分享所有者資訊,但根據商店回傳結果。
我嘗試了另一種使用商店檔案的方法,但根據我的特定要求,我最終得到了回圈依賴。
謝謝
uj5u.com熱心網友回復:
使用unwindonstores您可以展平檔案。之后,只需project您需要的欄位:
db.mycollection.aggregate( [
{ $unwind: "$stores" },
{ $project: { "ownerName": 1, "storeName": "$stores.storeName" } }
] )
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/410276.html
標籤:
