Unit 6 Database Progrmming with Nodejs and MongoDB
Unit 6 Database Progrmming with Nodejs and MongoDB
Compiled by:
Ms. Vaibhavi Parekh
Mongo DB
• MongoDB is a document database. It stores data in a
type of JSON format called BSON.
• A record in MongoDB is a document, which is a data
structure composed of key value pairs similar to the
structure of JSON objects.
• MongoDB is a document database and can be
installed locally or hosted in the cloud.
SQL vs Document Databases
• SQL databases are considered relational databases. They store
related data in separate tables. When data is needed, it is
queried from multiple tables to join the data back together.
• MongoDB is a document database which is often referred to as
a non-relational database. This does not mean that relational
data cannot be stored in document databases. It means that
relational data is stored differently. A better way to refer to it is
as a non-tabular database.
• MongoDB stores data in flexible documents. Instead of having
multiple tables you can simply keep all of your related data
together. This makes reading your data very fast.
• You can still have multiple groups of data too. In MongoDB,
instead of tables these are called collections.
Local vs Cloud Database
• MongoDB can be installed locally, which will allow you
to host your own MongoDB server on your hardware.
This requires you to manage your server, upgrades, and
any other maintenance.
• You can download and use the MongoDB open
source Community Server on your hardware for free.
• However, for this course we are going to use MongoDB
Atlas, a cloud database platform. This is much easier than
hosting your own local database.
• To be able to experiment with the code examples, you will
need access to a MongoDB database.
• Sign up for a free MongoDB Atlas account to get started.
Creating a Cluster
• After you have created your account, set up a free
"Shared Cluster" then choose your preferred cloud
provider and region.
• By default, MongoDB Atlas is completely locked down
and has no external access.
• You will need to set up a user and add your IP address to
the list of allowed IP addresses.
• Under "Database Access", create a new user and keep
track of the username and password.
• Next, under "Network Access", add your current IP
address to allow access from your computer.
Install MongoDB Shell (mongosh)
ObjectId(<hexadecimal>)
• Output:
• ObjectId(“5f92cbf10cf217478ba93561”)
Timestamp of the ObjectID
• It returns the timestamp information of the object as a
date in ISO format.
Converting ObjectId to string
• ObjectId can be converted into string format.
Install MongoDB On Windows
• To install MongoDB on Windows, first download the
latest release of MongoDB from
https://www.mongodb.com/download-center
Install MongoDB On Windows
Connectivity with nodejs
• Node.js and npm:Node.js is a JavaScript runtime for
building fast and scalable network applications, and it
comes with npm installed.
• MongoDB Node.js driver:To connect your application
with a database, you need a software component, also
known as a driver. MongoDB Node.js driver supports
database connection, authentication, CRUD operations,
and observations, among other features.
• MongoDB Atlas:You can create a database either locally
or in the cloud by using MongoDB Atlas, a fully
managed cloud database service.
• Initialize the Project
• First, create a project directory called “node-mongoDB”. Open your
terminal and execute the following command:
• mkdir node-mongoDB && cd node-mongoDB
• Once inside the directory, create a “package.json” file by running
the following command:
• npm init –y
• The “y” flag generates an npm package that uses the default values.
• In this project, we’ll use ECMAScript modules, which are
the standardized way to package JavaScript code. To enable
ECMAScript modules, add a new field to the “package.json” file:
• "type": "module“
• This will instruct Node.js to treat your JavaScript code as
ECMAScript modules instead of the default CommonJS modules.
Enabling ECMAScript modules will also allow us to use top-level
await. In short, this means that we can use the await keyword
outside of async functions.
Add MongoDB as a Dependency