Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k 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 (106k 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 (50.2k 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
2 answers
0 votes
1 answer

Browse Categories

...