1

Perform CRUD operation for Node with MongoDB using Mongoose – Part- 1

[Social9_Share type="horizontal"]

Perform CRUD operation for Node Application with MongoDB

In this post, let us learn how to connect to mongoDB using MongooseJS from a Node js application.As this post is very big, I have divided this into two parts. In Part-1, let us discuss these.

In Part-2 , we will modify app.js and perform CRUD operation.

Let’s get started.

Install MongoDB

Go to the official website of mongoDB to download it. Run the MongoDB .msi file and install mongoDB. MongoDB requires a data directory to store all data.The default path is C:/data/db.Now, MongoDB is installed locally in your system. Alternatively, you can also use mongolab to run mongoDB.

Start MongoDB instance

Open Command prompt and type the command mongod.exe. This starts the mongoDB instance The screenshot is shown below.

mongo-start

Launch Mongo shell

Open another command prompt and type mongo.exe. The Mongo Shell is now connected to the MongoDB instance.

mongo.connect

You can go through MongoDB documentation to learn more on Mongodb.

Create a Node Application using Express

Lets first create a Node application using Express framework. I have already given the steps here to create a web application. I am working on the same application now. You can remove the code from line 4 to line 12 as it is not relevant here. The screenshot of the sampleapp is given below.

mongo-app

What is Mongoose ?

Mongoose is MongoDB Object Modeling tool for Node js. It provides schema based solution to model application data. Mongoose has got built-in typecasting, query building validations and much more.

We will use mongoose to establish a connection with MongoDB, create a schema, make a model and perform CRUD operation.

Add Mongoose to the project

Now, we need to add mongoose to our application. To add mongoose to the application, open command prompt and navigate to the project folder and the type


mongo-mongoose

Don’t worry about the errors that you may get while installing mongoose as shown above. This will arise if Python is not installed in your system. But mongoose will be installed.

Create employee.js

Create a js file inside sampleapp/model and name it as employee.js. Inside this file let us do the following.

Let’s get started

Invoke Mongoose Module

Invoke the mongoose module by calling require method.

Connect to MongoDB

Next, let’s get a connection with the locally running MongoDB instance.

    where test is the database that I am connecting to.

Open a connection

Open a connection with MongoDB instance and log success or error.The callbacks will be invoked accordingly.

mongo-dbopen

Create the Schema

The Schema in mongoose maps to the collections in MongoDB. The Schema defines the structure of your documents. You can specify the attributes of the documents, that your collection takes in a schema. A schema can be created using

MongoDB uses JSON structure to create the schema. Mongoose Schema has its own schematypes like String, Number, Date, Array and so on.

Now, let us create our empSchema with the set of attributes. For my empSchema I want the attributes as name, username, city, mobile and other(this is nested and has designation and email)

.
mongo-empschema

Create the Model

Models are constructors, that are compiled from the schema. A document is an instance of the model. Creation, Retrieval, Updation, Deletion of documents are handled using model only. The model can created using the method

The first parameter is the name of the collection with which it will be stored in MongDB database.
The second parameter is the schema name that specifies the attributes. Now, let’s use our empSchema to create the model.

mongo-empmodel

So, here my collection name is employee. Once the model is created, lets export this model.

Export the Model

Export the model using module.exports method.

mongo-empexport

The complete code of employee.js is given below.

mongo-employee

All Set. Now, employee.js is ready with the connection to MongoDB, schema and model. Next, Lets move on to Part-2 to modify app.js and perform CRUD operation.

Post a comment