Teddy Hartanto
Teddy Hartanto

Reputation: 584

What is the difference between Model#remove and Model.remove in Mongoose?

In the Mongoose API docs, there is Model#remove, and there is Model.remove.

Can somebody explain, in layman terms, what is the difference between these two? They appears to accept different parameter(s). And why is one using dot and the other using hash? I need an even clearer explanation besides the one in the API docs.

Upvotes: 0

Views: 100

Answers (1)

robertklep
robertklep

Reputation: 203494

Model#remove is an instance method, Model.remove is a class method.

In other words, say that you have a model called Users. To remove something from the collection that belongs to that model, you have two options (this example is a bit contrived):

Users.findOne(CONDITION, function(err, user) {
  if (err) throw err;
  user.remove(function(err) {
    if (err) throw err;
    // user is removed
  });
});

This uses Model#remove: you have an instance of the model stored in user, and you remove that instance from the database by calling the remove method on the instance.

The other option:

Users.remove(COND, function(err) {
  if (err) throw err;
  // user_s_ matching COND have been removed
});

Basically, Model#remove is used to remove a single document for an instance that you already have, and Model.remove is used to remove possibly a list of documents that match a particular condition, or a document that you don't necessarily first want to retrieve from the database.

Upvotes: 0

Related Questions