Uploaded on Jan 27, 2020
Connect MongoDB with Node app using MongooseJS
Connect MongoDB with Node app
using MongooseJS
Connect the MongoDB database by using MongooseJS to the NodeJS application.
Before we dive into watching how the MongoDB connects with a Nodejs app using
mongooseJS, let's get a quick intro to these technologies which are ruling the online
development domain of today.
• Node: Node.js (Node) is an open-source development platform that is used for
executing JavaScript code server-side. Node is beneficial for developing applications
that need a persistent connection from the browser to the server and is usually used
for real-time applications like chat, news feeds, and web push notifications.
• MongoDB: MongoDB may be a cross-platform and open-source document-oriented
database, a sort of NoSQL database. As a NoSQL(not only SQL) database, MongoDB
reject the relational database’s table-based structure to adapt JSON-like documents
that have dynamic schemas which it calls BSON(Binary JSON).
• MongooseJS: Mongoose or MongooseJS may be a MongoDB object
modeling(ODM) tool designed to figure in an asynchronous environment.
Basically, it's a package that we'll use to interact(query, update,
manipulate) with our MongoDB database in our nodeJS app. we might
install or ‘require’ mongooseJS in our app with NPM(node packaging
manager)
• NPM: Node Package Manager or NPM is that the official package
manager for nodeJS applications. it'll come installed with NodeJS. it's used
from the instruction or terminal(depending on what OS is being used).
So now that we are well-known with the basic definitions of these
technologies, let's dive into the code and its explanations. Our NodeJS app
during this case(for demo purpose) goes to be one JavaScript file. Let’s call
it app.js. move ahead and create that file in a new folder.
Source code explanation:
Line2: It needs or imports the mongoose package in our app.
Line4: It assigns the connection string which contains the data about the connection to our MongoDB
variable.
Line6: These lines help in establishing or ‘open’ or stir up a reference to the database mentioned within
the MongoDB variable. the primary argument to the mongoose.connect() function is that the
connection string(the MongoDB variable).
Line10: The mongoose.connect() function returns the connection to the database
as mongoose.connection which we assign to the DB variable.
Line14: This line logs the message into the console when the connection to the
database has been created and came back. It listens for ‘connected’ event and
once the event fires, the function() comprising of line 14,15,16 gets executed.
Line19: This line logs the message into the console when the connection to the
database has been created and came back. It listens for the ‘error’ event and once
the event fires, the function() comprising of line 19,20,21 gets executed.
After writing the source code open a terminal or the command prompt(in case of
windows users) and navigate to your project directory. Then write the command
npm install mongoose as shown within the image below:
The above command will create a ‘node_modules’ folder in your current
directory or folder and download the required files there.
If you're following so far , all the preparation is completed and now we will
test our connection to database.
Write node app.js to start out the app.
For More information about Node JS
Call : +91 9989971070
Visit: https://www.visualpath.in/
www.visualpath.in
Thank you
www.visualpath.in
Comments