Chapter 4. Interacting with Data – an Introduction
We have seen how to define our data structure, but now we want to interact with it. We want to be able to create database entries, read them, update them, and delete them. To handle these interactions, Mongoose uses methods added to models. This chapter introduces the concept and will look at how to organize our code to best work with this approach.
By the end of this chapter, you will have had a glimpse of how Mongoose helps you to easily interact with your data. You will also have prepared the sample project so that we can dive into practical examples in later chapters.
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...
Before we get going on the Mongoose code, we need to get a couple of things in order in our project:
If we think in an MVC way, our Express project already has a model folder and a views folder. The model folder holds our schema definitions, Mongoose models and Mongoose connection. The views folder contains Jade templates for page layout. The controller aspect sits in the routes folder of a standard express installation. We could separate it out or move it around, but as the focus of this book is on using Mongoose, but not on MVC best practices, let's work with the default setup.
A good starting point when thinking about setting up your routes is to have one routes
file for each Mongoose model. Given the non-normalized data structure of MongoDB there aren't generally too many of these—we're not talking about individual tables in a relational database here! Also, as we saw earlier...
In this chapter, we have had a very quick glance at how Mongoose provides methods for us to interact with our data. We have also seen how to set up our project to work with Mongoose, attaching our schemas and models to the actual back end of the application.
In the next four chapters, we will be exploring the Mongoose data methods in much greater detail, focusing around the cornerstones of CRUD operations. First up, Chapter 5, Interacting with Data – Creation, for creating data.