Connect MongoDB with Node JS

Learn via video courses
Topics Covered

Overview

MongoDB is a NoSQL database for nodejs. We will use MongoDB driver for nodejs to manage MongoDB database. MongoDB uses binary JSON to store data. We will also use the mongoose tool to connect MongoDB with Node js and manage the database (i.e. create, read, update, and delete documents). Alike traditional databases, MongoDB is easy to use and saves time.

Pre-requisites

  • Basic understanding of relational databases like SQL.
  • Good understanding of javascript, objects, and data-types.
  • Nodejs and MongoDB database must be installed on your system.
  • Path for nodejs and MongoDB must be added to the PATH environment variable.

Introduction

Consider, We have a table schema defined in a relational database as shown below. We can not insert new data into the table that contains the new field phoneNumber. Because Field phoneNumber is not defined in the table schema.

But, MongoDB doesn't need a pre-defined schema. We can insert new data in the object format with any additional fields. MongoDB store data as a document shown below:

Therefore, MongoDB is different from relational databases. It comes in the category of NoSQL databases that uses JSON-like format to store documents. Relational database use table, raw, and column to store the data while NoSql Databases uses collection, and documents to store the data. the collection contains documents and documents store the data in object format.

Install the MongoDB Node.js Driver

  • Create a new file named mongodb-nodejs and go to the current folder mongodb-nodejs with CLI (Command Line Interface) as shown below:
  • Create a new node project with npm that add the package.json file inside mongodb-nodejs folder
  • Install the mongodb driver for nodejs to use the MongoDB database with nodejs
  • mongodb driver helps us to connect and easily manage queries in MongoDB with nodejs.

Note: In this article, We are using Nodejs version 16.17.0 and mongodb nodejs driver version 6.0.2. We can check the current version of nodejs using the command mongod --version and MongoDB nodejs driver using the command node --version.

Connecting to The Local MongoDB Database

  • We will define a path to store data for MongoDB on the local machine. We will add the path C:\Program Files\MongoDB\data\db. Also, We make sure that the specified path/folder exists.
  • In the above code block, We are using --dbpath to add a path for the MongoDB database and start the server locally.

Configuring The MongoDB Node.js Connection

  • Create a file named server.js and add the following code to the server.js file
  • Run the node server.js command and We will see the following output

Closing The Connection

  • We will replace the previous code of the server.js file with the following code:
  • In the above code block, We are using the close() method to disconnect the database from the node app.
  • Node app will exit from the MongoDB instance after connecting the node to the database.
  • Run node server.js

Example

  • First, We will install the following npm packages:

    • Nodemon: It watches file changes in nodejs and restarts the node app if any changes happen to files. We will install nodemon using the command npm install nodemon --save-dev.
    • Express: It is a nodejs framework that helps to build node APIs. We will install express using the command npm install express --save.
    • Mongoose: It is a Tool for the MongoDB database. It helps to create schema, model, and manage database queries for MongoDB. We will install mongoose using the command npm install mongoose --save.
    • Bodyparser: It will parse the data that is coming from the HTML body. We will install body-parser using the command npm install body-parser --save.
  • We will follow the file structure to create files and folders as shown below:

  • In package.json, We will add the start property to scripts.
  • We will require all modules to index.js file as shown below:
  • We will listen to the express app at local port 4000 and add the following code to the index.js file
  • We will add the following code to index.html. It is a simple student form that will get the data from the web and store the data in the local MongoDB database.
  • In the above code block, We are using the action and method attribute in the form tag. action attribute sends the data to the server and the method attribute tells whether it is a POST request or GET request.
  • We will tell express to serve the index.html on local port 4000 and use the body-parser to get the data from the HTTP request in a proper format.
  • In the above code block, We are enabling middlewares with the help of the use() method of the express app. We are using the static() method to add a path for static files.
  • We need a database to store the data. Hence, We will connect the local MongoDB database using the connect() method of mongoose.
  • In the above code block, We pass the database URL that creates the collection name students if it doesn't exist.
  • We will create and validate the schema using the Schema() method of mongoose. We will also create a model using the model() method of mongoose.
  • We will use the post() method of the express app to serve the HTTP request POST to the /student route.
  • Now, We will go to the URL http://localhost:4000/ and fill out the student form.

URL

  • When we hit submit button, It will store the student data in the local MongoDB database and navigate to the http://localhost:4000/student URL and display the output as shown below:
  • We will use the get() method to fetch all the students from the local MongoDB database.
  • In the above code block, We are using the find() method to retrieve students' data. When we hit the URL http://localhost:4000/students, It will display the output as shown below:

Insert Documents

Update/Delete Documents

  • In the above code block, We are using the updateOne() method to select the student using name or email and update it with the $set variable
  • In the above code block, We are using the deleteOne() method to select the student using name or email and delete it.

Mongoose

  • Mongoose is an ODM(Object Data Modeling) tool that helps to define the model based on the schema. A schema is a kind of structure that defines how we can store data in the database. It helps us to validate types like objects, strings, booleans, numbers, etc.
  • Consider, We want to make a list of npm libraries, then we will use the following format to store information in the database.
  • In this way, We can store data in the MongoDB database but it is not validated. Hence, We use mongoose to define a schema, validate data and create a model as shown below

-mongoose map our JSON data with the mongoose model, validate the data with a defined schema, and interacts with the MongoDB database with available methods in mongoose.

Ready to create blazing-fast applications? Enroll in our Free Node JS course and learn to optimize your web projects for speed.

Conclusion

  • MongoDB is a NoSQL database that uses collection and documents to store data.
  • mongodb driver for nodejs helps to connect and interact with the MongoDB database for nodejs.
  • We can install the mongodb driver for nodejs using the npm library.
  • We use MongoClient() to create a new instance of MongoDB.
  • We use the connect() method of MongoClient to connect with the database.
  • We can create, read, update and delete single/multiple documents in MongoDB.
  • Mongoose use the Schema() method to define a schema, the type property to validate data-types, and the model() method to create a model.
  • We can also manage documents in the MongoDB database using mongoose.