What is Node.js?

Node.js is a platform built on Chrome’s V8 JavaScript run time for easily building fast, scalable network applications. It helps build high performance network applications that are optimized for a concurrent environment.

Node uses  an event driven non-blocking IO which makes it lightweight hence very fast. It uses JavaScript’s callback functionality to implement the non-blocking IO.

One of the most basic requirements of any standard application is data persistence. This is where Mongodb comes into picture. MongoDB is among the top NoSQL databases out there. The fact that the data is stored in Mongo in the JSON format makes it one of the most compatible data stores for Node to work with. Lets take a look at how to go about connecting the two and performing basic operations on a Mongo database from Node. We will be using Mongoose to connect to Mongo. Mongoose along with other packages like Passport, Express are one of the most commonly used third party libraries used in Node.

What is Mongoose?

Mongoose is an object modeling package for Node. Mongoose allows us to have access to the MongoDB commands basic DB operations.

To use mongoose, first install mongoose using the command:

Add mongoose to the project by adding the below line to our app.js file

Now, the first step would be to establish a connection to MongoDB.

Eg:

Understanding Mongoose Schema and Models

In Mongodb, data is stored in documents, which in turn are stored in collections. Schema is what we use to define attributes for the document. All connection to data happens using schema’s. Model represents the documents which are to be saved to or retrieved from the database.

The schema by itself cannot do much until we create a corresponding model for it.

var emp= mongoose.model(‘Employee’, employeeSchema, ‘Employee’ );

An important lesson is about the third parameter in the above statement. In common practise, people tend to skip the third parameter as it is optional. In most cases the the collection name in Mongo will correspond to the first parameter. However if we do use

then, the corresponding collection created in MongoDB would be a pluralized version of the Model name, hence the actual collection that mongoose tries to work with in Mongo would be ‘employees’. This becomes a major problem in cases where in there are systems other than Node which intend to read or write from the ‘Employee’ collection. Hence it is good practice to always pass the collectionName parameter even though it is optional. Furthermore, suppose a collection called ‘Employees’ already exists, it is imperative to use the third parameter, as Mongo DB is case sensitive. Not using the third parameter would mean Mongoose will be trying to work with the ‘employees’ collection instead of the ‘Employees’ collection. To avoid such scenarios it is good practice to name Mongo collections in lowercase.

Adding records to Mongo

 

Reading records from Mongo

In the course of the application we may need to perform many kinds of reads to the database. Below are a few ways of performing different types of reads using Mongoose

 

Updating records 

 Deleting  records