dimanche 19 avril 2015

Mongoose ODM findOneAndUpdate sub-document

I currently have a Message schema which has a sub-document of replies as such:



message: String,
replies: [{
message: String,
userFrom: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
}]


I am attempting to make a POST request that will allow me to findOneAndUpdate the message to attach a reply. I've attempted the following:



Message.findOneAndUpdate(req.params.id,{
replies: {
message: req.body.reply,
userFrom: req.user._id
}
}, function(err, data){
...
});


What is happening is I am overwriting any reply that is currently in the replies array. I believe I need to use the $push operator but I'm not quite sure how.


Is it possible to use findOneAndUpdate as well as $push in a single request?


2 commentaires: