0 votes
1 view
in Web Technology by (47.8k points)

FBFriendModel.find({ 

id: 333 

}, function (err, docs) { 

docs.remove(); //Remove all the documents that match! 

});

The above doesn't seem to work. The records are still there.

Can someone fix?

2 Answers

0 votes
by (107k points)

To remove documents using Node.js Mongoose without iterating, you can try

FBFriendModel.find({ id:333 }).remove( callback );

Or

FBFriendModel.find({ id:333 }).remove().exec();

The mongoose.model.find method returns a Query, which has a remove function.

0 votes
by (37.8k points)

To remove a record, or document as it is called in MongoDB, we use the deleteOne() method.

The first parameter of the deleteOne() method is a query object defining which document you want to delete. remove() is deprecated and you can use deleteOne(), deleteMany(), or bulkWrite() instead.

But for the mongoose greater or equal to 2.7.1, you can also remove the document directly with the .remove() method rather than locating the document and then removing it which seems to be more efficient and easy to maintain.

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...