Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript last element array 
Javascript :: js add to array 
Javascript :: javascript array loop 
Javascript :: get yyyy-mm-dd hh:mm from date javascript 
Javascript :: js reduce method 
Javascript :: function to generate random number in javascript 
Javascript :: jsp date 
Javascript :: delete item from array 
Javascript :: javascript trigger keypress 
Javascript :: jquery event methods 
Javascript :: react hook form clear form 
Javascript :: angular decorators list 
Javascript :: array destructuring js 
Javascript :: decimal to base 32 javascript 
Javascript :: head component nextjs 
Javascript :: vue font awesome icons 
Javascript :: React native calender date picker 
Javascript :: async storage react native 
Javascript :: react-slick 
Javascript :: How to end a session in ExpressJS 
Javascript :: add class to element vue 
Javascript :: Generate a Random Integer 
Javascript :: then and catch in promise 
Javascript :: WebPack basic Configuration 
Javascript :: Get the Last Items in an Array 
Javascript :: check if array contain the all element javascript 
Javascript :: if or react 
Javascript :: js do while 
Javascript :: array flatten 
Javascript :: max method in js 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =