![](/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.