简体   繁体   English

请问如何更新嵌套的 mongodb 嵌入文档,我有这样的文档结构

[英]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"
}
}

}

I want to update the photo field as an embedded schema under profile which is an embedded schema to user.我想将照片字段更新为配置文件下的嵌入式架构,这是用户的嵌入式架构。

With Mongoose driver we can easily update nested documents.使用 Mongoose 驱动程序,我们可以轻松更新嵌套文档。

Lets say you have fectched the document with mongoose, like below.假设您已经使用猫鼬修改了文档,如下所示。

let document = await model.find ({ _id : '5f5fd12521a9b27e849d9b7b'});让文档 = 等待 model.find ({ _id : '5f5fd12521a9b27e849d9b7b'});

Now you can simply assign the new data just like an object.现在您可以像分配对象一样简单地分配新数据。

document.profile.photo= {...somestate}; document.profile.photo= {...somestate}; await document.save();等待 document.save();

That's it.就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM