How do I remove documents using Node.js Mongoose ?

How do I remove documents using Node.js Mongoose ?

Asked on December 27, 2018 in MongoDB.
Add Comment


  • 3 Answer(s)

    You can using this code to solve your issue:

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

    mongoose.model.find to returns a Query and  which has to be an remove function.

    Answered on December 27, 2018.
    Add Comment

    For that “mongoose”: “>=2.7.1” you could delete the document straightly to using remove method rather than finding the document and then removing it seems to maintan

    For Example:

    Model.remove({ _id: req.body.id }, function(err) {
        if (!err) {
              message.type = 'notification!';
        }
        else {
              message.type = 'error';
        }
    });
    

    UPDATING

    The mongoose 3.8.1 contain a many methods that lets you delete directly a document, They are:

      • remove
      • findByIdAndRemove
      • findOneAndRemove

    You need a more information you can refer this mongoose API docs

     

    Answered on December 27, 2018.
    Add Comment

    Array of document is docs, so it doesn’t have a mongooseModel.remove() method.

    To delete every document in the array separately.

    Or – For that we are finding the documents by a (probably) unique id – use findOne instead of find.

     

     

    Answered on December 27, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.