Model methods and instance methods
As we have seen in Chapter 3, Schemas and Models, a document in a Mongoose collection is a single instance of a model. So it makes sense that if we're going to work with our data then it will be through the model.
So what do we mean by model methods? Well, if we have a model called User
, some of the methods provided by Mongoose are User.create
, User.find
, User.update
, and User.remove
. The method names may be slightly different, but out of the box we've got methods for all four CRUD (Create, Read, Update, and Delete) operations right there.
Instance methods are the same concept, except that they are applied to specific instances instead. We will look at a good example of this soon, with the instance.save
method.
Mongoose also allows you to define your own model methods, giving you the flexibility to create the functionality you want. We'll see more about these static methods and create one ourselves in Chapter 6, Interacting with Data – Reading, Querying, and...