简体   繁体   中英

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.

Lets say you have fectched the document with mongoose, like below.

let document = await model.find ({ _id : '5f5fd12521a9b27e849d9b7b'});

Now you can simply assign the new data just like an object.

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

That's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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