我有這樣的檔案,
{
"_id": 1,
"attribute1":"123",
"attribute2":"abc",
"subDocument": [
{
"attribute1":"456",
"attribute2":"xyz",
}
]
}
我想將第一個專案subDocument提升到頂級。請注意, subDocument 的專案與頂級專案具有相同的屬性,我想替換它們。所以結果將是
{
"_id": 1,
"attribute1":"456",
"attribute2":"xyz",
}
我應該如何撰寫查詢?游樂場https://mongoplayground.net/p/x0pzzwdi5k9
uj5u.com熱心網友回復:
詢問
- 獲得第一個成員
- 將其與根合并,并替換根
- 相同的名稱將從第一個成員中替換
測驗代碼在這里
db.collection.update({},
[{"$replaceRoot":
{"newRoot":
{"$mergeObjects":
["$$ROOT", {"$arrayElemAt": ["$subDocument", 0]}]}}},
{"$unset": ["subDocument"]}])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/330087.html
標籤:MongoDB
上一篇:一文講清MySQL四種隔離級別
