Answers for "mongoose find subdocument in array by id"

1

mongoose update subdocument by id

//this method for add data to subdocument
BlogPost.findById(req.params.postId, function (err, post) {
    var subDoc = post.comments.id(req.params.commentId);
    subDoc = req.body;
    post.save(function (err) {
        if (err) return res.status(500).send(err);
        res.send(post);
    });
});

// alternative second method you can use this
BlogPost.findOneAndUpdate({_id: req.params.postId}, {$push:{ subDoc: req.body }}, (err, doc) => {
  	// do something here
});
Posted by: Guest on July-26-2020
0

mongoose nested object without id

var subSchema = mongoose.Schema({
    //your subschema content
},{ _id : false });
Posted by: Guest on February-27-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language