Defining custom model methods

Mongoose models are quite packed with both static and instance predefined methods, some of which you have already used. However, Mongoose also lets you define your own custom methods to empower your models, giving you a modular tool to separate your application logic properly. Let's go over the proper way of defining these methods.

Defining custom static methods

Model static methods give you the liberty to perform model-level operations, such as adding extra find methods. For instance, let's say you want to search users by their username. You could, of course, define the this method in your controller, but that wouldn't be the right place for it. What you're looking for is a static model method. To add a static method, ...

Get MEAN Web Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.