问题
First, my code is working well, but i don t know if doing the right thing. I have a parent model Units with this schema
{
info: {...},
chapters: {_id: string, ...}
}
the chapter schema is like this:
{
info: {...},
content: string
}
now in the client if i am editing the a chapter, and want to put the changes to the database, i do it like this
Units.findOne({'chapters._id': chapter_id}) //this query will return the parent unit
.then(unit=>{ //is there a way to have the chapter with unit here??
const chapter = unit.chapters.find(chapter=> chapter._id === chapter_id)
//seems like duplicated. i already found the unit based on the chapter_id
//after that i update the chapter
})
that's it, I wish there is a way to return the chapter in the first query
来源:https://stackoverflow.com/questions/57745539/getting-the-sub-document-with-the-document-in-mongoose-findone