getting the sub-document with the document in mongoose.findOne

ぐ巨炮叔叔 提交于 2019-12-11 19:05:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!