express mongodb
Why use MongoDB/Mongoose
Node.js and MongoDB are a pair made for each other. Being able to use JSON across the board and JavaScript makes development very easy.
Mongoose is an object modeling package for Node that essentially works like an ORM.
Mongoose allows us to have access to the MongoDB commands for CRUD simply and easily.
Adding Mongoose to Express Application
MongoDB provide native driver for Node.js called “mongodb” which you can use to connect to MongoDB and perform various CRUD operation. There is another more popular MongoDB recommended node module called “mongoose” and this one we are going to use.
First of all, you need install Mongoose in your project (package.json) like any other dependency — using NPM. To install it, use the following command inside your project folder:
NOTE: Installing Mongoose adds all its dependencies, including the MongoDB database driver, but it does not install MongoDB itself. If you want to install a MongoDB server then you can download installers from here for various operating systems and install it locally. You can also use cloud-based MongoDB instances.
Then you need to setup the connection for the mongodb using mongoose in your project, see /config/database.js
as below:
NOTE: In order to run setup the connect from your express app to mongodb, you have to run the mongodb in your local first by mongod
, then you will see something like waiting for connections on port 27017
in your terminal, which means mongodb is up and running in your local machine. If you prefer using a cloud mongodb host, mLab is one of the most popular cloud database service and you have the option to get the Sandbox Plan for free.
CRUD And RestFul API
CRUD is an acronym for Create, Read, Update and Delete. It is a set of operations we get servers to execute (POST, GET, PUT and DELETE respectively). This is what each operation does:
Create (POST) - Make something
Read (GET)_- Get something
Update (PUT) - Change something
Delete (DELETE)- Remove something
Define Models
Models are defined using the Schema interface. The Schema allows you to define the fields stored in each document along with their validation requirements and default values.
Let's create a Schema for our first model User
:
Each key in our code userSchema defines a property in our documents which will be cast to its associated SchemaType. For example, we've defined a property username
which will be cast to the String SchemaType, as well as it cannot be empty and it has to be the unique value.
Following are all valid Schema Types.
String
Number
Date
Buffer
Boolean
Mixed
Objectid
Array
Here is the link to the official documentation for SchemaType
, where you can find details for other parameters for a property in the Schema
.
Then let's create the User
model using the userSchema
:
So now we have our first model User
, what can we do with the new model?
Read
Find all
Find one
Find by ID
Update
Get and then update
Find and then update
Find by id and update
Delete
Get then remove
Find then remove
Find by Id then remove
Run a function before saving
We also want to have a created_at
variable to know when the record was created, as well as a update_at
variable to know when the record was updated . We can use the Schema pre
method to have operations happen before an object is saved.
Last updated
Was this helpful?