Mongoose findOneAndUpdate — updating an object inside an array of objects

孤人 提交于 2019-12-30 11:26:48

问题


Take a look at this Schema:

let userSchema = new Schema({
email: { type: String, required: true },
password: { type: String, required: true },
books: [{
    cover: String,
    title: String,
    link: String,
    requests: { requestingUser: String, bookOffered: String, 
    beingRequested: false }
}],
ip: String
});

What I am trying to do is use findOneAndUpdate to (based on given information) find the correct user, and then find the correct book (from the array of books by given title) And update the requests field (which is another object.) Is this possible to accomplish with only one findOneAndUpdate query ?

So far my finding has been that I would have to do a findOne to get the correct user, and then do a findOneAndUpdate nested inside that. Is there a way to do this with one query command?

Thanks

来源:https://stackoverflow.com/questions/43128561/mongoose-findoneandupdate-updating-an-object-inside-an-array-of-objects

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