Lesson 17. Improving Your Data Models

In this lesson, you take advantage of Mongoose’s schema- and model-creation tools. To start, you improve on your simple model and add properties to the models to restrict what data can be saved to the database. Next, you see how to associate data in a NoSQL database such as MongoDB. At the end, you build out static and instance methods for the model. You can run these methods directly on Mongoose model objects, and create the necessary controller actions for them to work with the application.

This lesson covers

  • Adding validations to your models
  • Creating static and instance methods for your models
  • Testing your models in REPL
  • Implementing data associations on multiple models
Consider this

You’ve set up a form for people visiting your recipe application to subscribe to a newsletter. Now you want to populate your application with courses in which users will be able to enroll and learn to cook.

With the help of Mongoose, you’ll be able to set up your models so that subscribers can show interest in a particular program before signing up as users.

17.1. Adding validations on the model

So far, you’ve built a model with Mongoose. The model you created is an abstraction from the data, represented as a document, in your MongoDB database. Because of this abstraction, you can create a blueprint of how you want your data to look and behave using Mongoose schemas.

17.2. Testing models in REPL

17.3. Creating model associations

17.4. Populating data from associated models

Summary

sitemap