Mongoose, Schemas, Models

Daniel Glover
4 min readJul 29, 2019

Connecting to a database using Mongoose and building a model.

When writing Node.js applications, we have a plethora of considerations regarding writing to a database. One of which, is the NoSQL database MongoDB. In this write up, I will be examining and explaining the usage of Mongoose, specifically how we can build schemas for our collections and perform operations and interactions to/with our application using this ODM.

An ODM is an Object Data Model, using Object Document Mapping. It presents application’s data as Javascript Objects which are then parsed and mapped to the database.

There really is a multitude of options when considering ORMs/ODMs, they can — with little effort — be found on the Node Package Manager (NPM) site. In this blog, as I mentioned before, I will be focusing on Mongoose.

Mongoose & MongoDB go hand in hand, and are the go-to choice in the Node community, as the document storage and query system is, on the surface, almost exactly like JSON.

In MongoDB, a NoSQL database, we use document oriented data modelling. We create a model, a skeleton of what each creation should contain and look like. We call a Model’s creation a Document and a grouping of documents is called a Collection. It is akin to Rows in a Table for a relational database.

Let us now dive in, how exactly do we create & define our models?

First and foremost, we have to connect Mongoose to our MongoDB database. We need to make sure we have Mongoose installed (npm i mongoose), and then require it into our file (making sure to store the result in a variable for the next step). We can call our variable mongoose, the same as what we’d required into the file, so we have a clear indication of what it represents.
Now we can begin the connection process. Looking at the npm page for mongoose, we can find that there are multiple ways of connecting our Mongoose in the application to our MongoDB database. I’m going to use .connect, it is a method that can be called onto our mongoose variable using dot notation and it requires 2 arguments. The first argument is our MongoDB url, and the second is an object. The object has a key of UseNewUrlParser and it is set to true, we’re adding this as the original Url Parser is now deprecated and it is better to tell Mongoose to connect using the new Url Parser. Inside this object we can also provide other options, for example useCreateIndex and set that to true, or even remove already existing methods and set them to false, like other deprecated methods.

Back on track, Models.

We declare and define our models using Schemas. A Schema allows you to design your model’s fields for each document, and offers quite the expansive validation and value definition toolset. We can also set default values, lock in what type the data should be and also connect a Schema to another Schema (but we’ll look at that another time).

Schemas are compiled into models using the mongoose.model() method, and once it’s created/exists we can now find, create, update and delete documents/objects belonging to this model.

In Practise

There are 3 main stages to creating a model. We first build our Schema, then we compile a model from our schema and lastly (and optionally), we can export our Model to our router files for actions, operations and functionality.

I’ve given my BookSchema a field of title, which means that any document submitted must have a title and it also must be a data type of string in order to be processed and integrated into the Book Collection.

The mongoose.model method takes in two arguments, first is the name for referencing (a singular name of the collection that will be created for your model), and lastly the Schema we’re using to create this model.

Now we can design a process in our routers to create and save a book to our database. We do this by instantiating a new Book with the relevant fields and then saving that instance to the table.

There ya go! See you soon.

--

--