mongoose remove from array code example
Example 1: $pull mongoose
TemplateDoc.findOneAndUpdate(
{ userId: _id },
{ $pull: { templates: { _id: templateid } } },
{ new: true }
)
.then(templates => console.log(templates))
.catch(err => console.log(err));
Example 2: mongoose remove element from array
db.survey.update( // select your doc in moongo
{ }, // your query, usually match by _id
{ $pull: { results: { $elemMatch: { score: 8 , item: "B" } } } }, // item(s) to match from array you want to pull/remove
{ multi: true } // set this to true if you want to remove multiple elements.
)
Example 3: mongoose remove document from array
Favorite.updateOne( {cn: req.params.name}, { $pullAll: {uid: [req.params.deleteUid] } } )
Example 4: mongoose delete object from array
doc.subdocs.push({ _id: 4815162342 }) // added
doc.subdocs.pull({ _id: 4815162342 }) // removed