4

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

JohnSnow
  • 6,911
  • 8
  • 23
  • 44
  • 4
    Something like `findOneAndUpdate({"email" : emailvalue, "books.title" : titlevalue}, { "books.$.requests.requestingUser" : uservalue }, ...` – s7vr Mar 30 '17 at 21:35
  • Yea it worked now, don't know why it wasn't working before. – JohnSnow Mar 30 '17 at 21:41
  • 1
    Possible duplicate of [Mongodb : array element projection with findOneAndUpdate doesn't work?](http://stackoverflow.com/questions/18986505/mongodb-array-element-projection-with-findoneandupdate-doesnt-work) – s7vr Mar 30 '17 at 21:42

0 Answers0