![](/img/trans.png)
[英]How can I use variables in an update operation in mongodb that involves an array or another type of embedded or nested document?
[英]Please how can i update a nested mongodb embedded document, I have a document structure like this
user: {
_id: objectId("5f5fd12521a9b27e849d9b7b")
profile:{
_id: objectId("5f5fd12521a9b27e849d9b7b")
firstName: "Vincent"
photo:{
_id: objectId("5f5fd12521a9b27e849d9b7b")
thumbnail: "/pictures/image.png"
original: "/pictures/original/image.png"
}
}
}
我想將照片字段更新為配置文件下的嵌入式架構,這是用戶的嵌入式架構。
使用 Mongoose 驅動程序,我們可以輕松更新嵌套文檔。
假設您已經使用貓鼬修改了文檔,如下所示。
讓文檔 = 等待 model.find ({ _id : '5f5fd12521a9b27e849d9b7b'});
現在您可以像分配對象一樣簡單地分配新數據。
document.profile.photo= {...somestate}; 等待 document.save();
就是這樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.