SlideShare a Scribd company logo
- SHASHANK
What is
MongoDB?
MongoDB is a document-oriented NoSQL
database used for high volume data storage.
MongoDB is a database which came into light
around the mid-2000s. It falls under the category
of a NoSQL database. Unlike in SQL databases,
where you must have a table's schema declared
before inserting data, MongoDB's collections do
not enforce document structure.This sort of
flexibility is what makes MongoDB so powerful.
MongoDB
Features
1. Each database contains collections which in turn contains
documents. Each document can be different with a varying
number of fields.The size and content of each document can
be different from each other.
2. The document structure is more in line with how developers
construct their classes and objects in their respective
programming languages. Developers will often say that their
classes are not rows and columns but have a clear structure
with key-value pairs.
3. As seen in the introduction with NoSQL databases, the rows
(or documents as called in MongoDB) doesn't need to have a
schema defined beforehand. Instead, the fields can be created
on the fly.
4. The data model available within MongoDB allows you to
represent hierarchical relationships, to store arrays, and other
more complex structures more easily.
Key
Components
of MongoDB
Architecture
1. _id – This is a field required in every MongoDB
document. The _id field represents a unique value in
MongoDB document. The _id field is like the
primary key. If you create a new document without
MongoDB will automatically create the field. So for
we see the example of the above customer table,
will add a 24 digit unique identifier to each
collection.
2.Collection – This is a grouping of MongoDB
documents. A collection is the equivalent of a table
created in any other RDMS such as Oracle or MS SQL.
collection exists within a single database. As seen
introduction collections don't enforce any sort of
3.Cursor – This is a pointer to the result set of a query.
Clients can iterate through a cursor to retrieve results.
Key
Components
of MongoDB
Architecture
1. Database – This is a container for collections like in RDMS
wherein it is a container for tables. Each database gets its
files on the file system. A MongoDB server can store
databases.
2.Document - A record in a MongoDB collection is basically
called a document. The document, in turn, will consist of
and values.
3.Field - A name-value pair in a document. A document has
zero or more fields. Fields are analogous to columns in
databases.The following diagram shows an example of
Key value pairs. So in the example below CustomerID and
the key value pair's defined in the document.
4.JSON – This is known as JavaScript Object Notation. This is
a human-readable, plain text format for expressing
JSON is currently supported in many programming
Why Use
MongoDB
1.Document-oriented – Since MongoDB is a NoSQL type
database, instead of having data in a relational type
format, it stores the data in documents. This makes
MongoDB very flexible and adaptable to real business
world situation and requirements.
2.Ad hoc queries - MongoDB supports searching by field,
range queries, and regular expression searches. Queries
can be made to return specific fields within documents.
3.Indexing - Indexes can be created to improve the
performance of searches within MongoDB. Any field in a
MongoDB document can be indexed.
Why Use
MongoDB
1.Replication - MongoDB can provide high availability
with replica sets. A replica set consists of two or
DB instances. Each replica set member may act in
the primary or secondary replica at any time. The
replica is the main server which interacts with the
performs all the read/write operations. The
maintain a copy of the data of the primary using
replication. When a primary replica fails, the replica
automatically switches over to the secondary and
becomes the primary server.
2.Load balancing - MongoDB uses the concept of
sharding to scale horizontally by splitting data
MongoDB instances. MongoDB can run over multiple
balancing the load and/or duplicating data to keep
up and running in case of hardware failure.
Install
MongoDB
 CMD command to install mongodb after Download
it:
npm install mongodb --save
Create
Database
 To create a database in MongoDB, First create a
MongoClient object and specify a connection URL with
the correct ip address and the name of the database
which you want to create.
 MongoDB will automatically create the database if it
does not exist, and make a connection to it.
Example
 Create a folder named "MongoDatabase" as a database.
Suppose you create it on Desktop. Create a js file named
"createdatabase.js" within that folder and having the
following code:
Example
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/MongoDatbase";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
Create
Collection
 MongoDB is a NoSQL database so data is stored in
collection instead of table. createCollection
method is used to create a collection in MongoDB.
 Example
 Create a collection named "employees".
Example
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/ MongoDatabase";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.createCollection("employees", function(err, re) {
if (err) throw err;
console.log("Collection is created!");
db.close();
});
});
Insert Record
insertOne()
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/ MongoDatabase";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var myobj = { name: "Ajeet Kumar", age: "28", address: "De
lhi" };
db.collection("employees").insertOne(myobj, function(err, res
)
{
if (err) throw err;
console.log("1 record inserted");
db.close();
});
});
Insert Multiple
Records
insert()
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/ MongoDatabase";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var myobj = [
{ name: "Mahesh Sharma", age: "25", address: "Ghaziabad"},
{ name: "Tom Moody", age: "31", address: "CA"},
{ name: "Zahira Wasim", age: "19", address: "Islamabad"},
{ name: "Juck Ross", age: "45", address: "London"}
];
db.collection("customers").insert(myobj, function(err, res) {
if (err) throw err;
console.log("Number of records inserted: " + res.insertedCount);
db.close();
});
});
Select Record
findOne()
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/MongoDatabase";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.collection("employees").findOne({}, function(err, result) {
if (err) throw err;
console.log(result.name);
db.close();
});
});
Select Multiple
Records
find()
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/MongoDatabase";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
db.collection("employees").find({}).toArray(function(err, result) {
if (err) throw err;
console.log(result);
db.close();
});
});
Filter Query
find()
This method is also used to filter the result on a specific parameter.
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/MongoDatabase";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var query = { address: "Delhi" };
db.collection("employees").find(query).toArray(function(err, result)
{
if (err) throw err;
console.log(result);
db.close();
});
});
Filter With Regular
Expression
Retrieve the record from the collection where address start with
letter "L".
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/MongoDatabase";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var query = { address: /^L/ };
db.collection("employees").find(query).toArray(function(err, result)
{
if (err) throw err;
console.log(result);
db.close();
});
});
Sorting
sort()
Sort in Ascending Order
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/ MongoDatabase";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var mysort = { name: 1 };
db.collection("employees").find().sort(mysort).toArray(function(err, result)
{
if (err) throw err;
console.log(result);
db.close();
});
});
Remove
remove()
var http = require('http');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/ MongoDatabase";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var myquery = { address: 'Ghaziabad' };
db.collection("employees").remove(myquery, function(err, obj) {
if (err) throw err;
console.log(obj.result.n + " record(s) deleted");
db.close();
});
});

More Related Content

PPTX
Kalp Corporate MongoDB Tutorials
PDF
MongoDB
PPTX
Top 10 frameworks of node js
PPTX
Mongo db workshop # 01
PDF
Mongo db basics
PPTX
Mongo db Quick Guide
KEY
Mongodb intro
PPT
Intro to mongo db
Kalp Corporate MongoDB Tutorials
MongoDB
Top 10 frameworks of node js
Mongo db workshop # 01
Mongo db basics
Mongo db Quick Guide
Mongodb intro
Intro to mongo db

What's hot (20)

PPTX
PPTX
Mongo db
PPTX
Basics of MongoDB
PPTX
MongoDB presentation
PDF
Mongodb Introduction
PPTX
Mongo DB Presentation
DOCX
Mongo db report
PPTX
Mongo DB 102
PDF
An introduction to MongoDB
PPTX
MongoDB basics & Introduction
DOCX
MongoDB DOC v1.5
PPTX
NOSQL and MongoDB Database
PPTX
MongoDB for Beginners
PDF
Introduction to MongoDB
ODP
Introduction to MongoDB
PPTX
CMS Mongo DB
PPTX
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
PDF
Mongo db dhruba
PPTX
OVERVIEW OF MONGODB | CREATING USER IN MONGODB & ASSIGNING ROLES
Mongo db
Basics of MongoDB
MongoDB presentation
Mongodb Introduction
Mongo DB Presentation
Mongo db report
Mongo DB 102
An introduction to MongoDB
MongoDB basics & Introduction
MongoDB DOC v1.5
NOSQL and MongoDB Database
MongoDB for Beginners
Introduction to MongoDB
Introduction to MongoDB
CMS Mongo DB
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo db dhruba
OVERVIEW OF MONGODB | CREATING USER IN MONGODB & ASSIGNING ROLES
Ad

Similar to Mongo DB (20)

PPTX
Mongo db
PDF
MongoDB - An Introduction
PPTX
MongoDB introduction features -presentation - 2.pptx
PDF
Mongodb By Vipin
PPTX
mongodb11 (1) (1).pptx
PPT
MongoDB
PPTX
Top MongoDB interview Questions and Answers
PDF
Mongo learning series
PPTX
05201349_Unit_7_FSWD_ advanced learning.pptx
PPTX
05201349_Unit_7_FSWD_II(1) with advance.pptx
PPTX
05201349_Unit_7_FSWD_ advanced learning.pptx
PPTX
05201349_Unit_7_FSWD_II(1) with advance.pptx
PDF
MongoDB Interview Questions PDF By ScholarHat
PPTX
Introduction to MongoDB.pptx
PDF
MongoDB Interview Questions PDF By ScholarHat
PDF
3-Mongodb and Mapreduce Programming.pdf
PPTX
Mongodb Introduction
PPTX
Introduction to MongoDB
PPTX
Mongo db
MongoDB - An Introduction
MongoDB introduction features -presentation - 2.pptx
Mongodb By Vipin
mongodb11 (1) (1).pptx
MongoDB
Top MongoDB interview Questions and Answers
Mongo learning series
05201349_Unit_7_FSWD_ advanced learning.pptx
05201349_Unit_7_FSWD_II(1) with advance.pptx
05201349_Unit_7_FSWD_ advanced learning.pptx
05201349_Unit_7_FSWD_II(1) with advance.pptx
MongoDB Interview Questions PDF By ScholarHat
Introduction to MongoDB.pptx
MongoDB Interview Questions PDF By ScholarHat
3-Mongodb and Mapreduce Programming.pdf
Mongodb Introduction
Introduction to MongoDB
Ad

Recently uploaded (20)

PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPT
ISS -ESG Data flows What is ESG and HowHow
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
Data_Analytics_and_PowerBI_Presentation.pptx
PDF
Lecture1 pattern recognition............
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPT
Miokarditis (Inflamasi pada Otot Jantung)
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
ISS -ESG Data flows What is ESG and HowHow
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
Acceptance and paychological effects of mandatory extra coach I classes.pptx
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
Reliability_Chapter_ presentation 1221.5784
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Data_Analytics_and_PowerBI_Presentation.pptx
Lecture1 pattern recognition............
oil_refinery_comprehensive_20250804084928 (1).pptx
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Qualitative Qantitative and Mixed Methods.pptx
Supervised vs unsupervised machine learning algorithms
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Business Acumen Training GuidePresentation.pptx
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Miokarditis (Inflamasi pada Otot Jantung)

Mongo DB

  • 2. What is MongoDB? MongoDB is a document-oriented NoSQL database used for high volume data storage. MongoDB is a database which came into light around the mid-2000s. It falls under the category of a NoSQL database. Unlike in SQL databases, where you must have a table's schema declared before inserting data, MongoDB's collections do not enforce document structure.This sort of flexibility is what makes MongoDB so powerful.
  • 3. MongoDB Features 1. Each database contains collections which in turn contains documents. Each document can be different with a varying number of fields.The size and content of each document can be different from each other. 2. The document structure is more in line with how developers construct their classes and objects in their respective programming languages. Developers will often say that their classes are not rows and columns but have a clear structure with key-value pairs. 3. As seen in the introduction with NoSQL databases, the rows (or documents as called in MongoDB) doesn't need to have a schema defined beforehand. Instead, the fields can be created on the fly. 4. The data model available within MongoDB allows you to represent hierarchical relationships, to store arrays, and other more complex structures more easily.
  • 4. Key Components of MongoDB Architecture 1. _id – This is a field required in every MongoDB document. The _id field represents a unique value in MongoDB document. The _id field is like the primary key. If you create a new document without MongoDB will automatically create the field. So for we see the example of the above customer table, will add a 24 digit unique identifier to each collection. 2.Collection – This is a grouping of MongoDB documents. A collection is the equivalent of a table created in any other RDMS such as Oracle or MS SQL. collection exists within a single database. As seen introduction collections don't enforce any sort of 3.Cursor – This is a pointer to the result set of a query. Clients can iterate through a cursor to retrieve results.
  • 5. Key Components of MongoDB Architecture 1. Database – This is a container for collections like in RDMS wherein it is a container for tables. Each database gets its files on the file system. A MongoDB server can store databases. 2.Document - A record in a MongoDB collection is basically called a document. The document, in turn, will consist of and values. 3.Field - A name-value pair in a document. A document has zero or more fields. Fields are analogous to columns in databases.The following diagram shows an example of Key value pairs. So in the example below CustomerID and the key value pair's defined in the document. 4.JSON – This is known as JavaScript Object Notation. This is a human-readable, plain text format for expressing JSON is currently supported in many programming
  • 6. Why Use MongoDB 1.Document-oriented – Since MongoDB is a NoSQL type database, instead of having data in a relational type format, it stores the data in documents. This makes MongoDB very flexible and adaptable to real business world situation and requirements. 2.Ad hoc queries - MongoDB supports searching by field, range queries, and regular expression searches. Queries can be made to return specific fields within documents. 3.Indexing - Indexes can be created to improve the performance of searches within MongoDB. Any field in a MongoDB document can be indexed.
  • 7. Why Use MongoDB 1.Replication - MongoDB can provide high availability with replica sets. A replica set consists of two or DB instances. Each replica set member may act in the primary or secondary replica at any time. The replica is the main server which interacts with the performs all the read/write operations. The maintain a copy of the data of the primary using replication. When a primary replica fails, the replica automatically switches over to the secondary and becomes the primary server. 2.Load balancing - MongoDB uses the concept of sharding to scale horizontally by splitting data MongoDB instances. MongoDB can run over multiple balancing the load and/or duplicating data to keep up and running in case of hardware failure.
  • 8. Install MongoDB  CMD command to install mongodb after Download it: npm install mongodb --save
  • 9. Create Database  To create a database in MongoDB, First create a MongoClient object and specify a connection URL with the correct ip address and the name of the database which you want to create.  MongoDB will automatically create the database if it does not exist, and make a connection to it. Example  Create a folder named "MongoDatabase" as a database. Suppose you create it on Desktop. Create a js file named "createdatabase.js" within that folder and having the following code:
  • 10. Example var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/MongoDatbase"; MongoClient.connect(url, function(err, db) { if (err) throw err; console.log("Database created!"); db.close(); });
  • 11. Create Collection  MongoDB is a NoSQL database so data is stored in collection instead of table. createCollection method is used to create a collection in MongoDB.  Example  Create a collection named "employees".
  • 12. Example var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/ MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; db.createCollection("employees", function(err, re) { if (err) throw err; console.log("Collection is created!"); db.close(); }); });
  • 13. Insert Record insertOne() var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/ MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var myobj = { name: "Ajeet Kumar", age: "28", address: "De lhi" }; db.collection("employees").insertOne(myobj, function(err, res ) { if (err) throw err; console.log("1 record inserted"); db.close(); }); });
  • 14. Insert Multiple Records insert() var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/ MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var myobj = [ { name: "Mahesh Sharma", age: "25", address: "Ghaziabad"}, { name: "Tom Moody", age: "31", address: "CA"}, { name: "Zahira Wasim", age: "19", address: "Islamabad"}, { name: "Juck Ross", age: "45", address: "London"} ]; db.collection("customers").insert(myobj, function(err, res) { if (err) throw err; console.log("Number of records inserted: " + res.insertedCount); db.close(); }); });
  • 15. Select Record findOne() var http = require('http'); var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; db.collection("employees").findOne({}, function(err, result) { if (err) throw err; console.log(result.name); db.close(); }); });
  • 16. Select Multiple Records find() var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; db.collection("employees").find({}).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });
  • 17. Filter Query find() This method is also used to filter the result on a specific parameter. var http = require('http'); var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var query = { address: "Delhi" }; db.collection("employees").find(query).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });
  • 18. Filter With Regular Expression Retrieve the record from the collection where address start with letter "L". var http = require('http'); var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var query = { address: /^L/ }; db.collection("employees").find(query).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });
  • 19. Sorting sort() Sort in Ascending Order var http = require('http'); var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/ MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var mysort = { name: 1 }; db.collection("employees").find().sort(mysort).toArray(function(err, result) { if (err) throw err; console.log(result); db.close(); }); });
  • 20. Remove remove() var http = require('http'); var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/ MongoDatabase"; MongoClient.connect(url, function(err, db) { if (err) throw err; var myquery = { address: 'Ghaziabad' }; db.collection("employees").remove(myquery, function(err, obj) { if (err) throw err; console.log(obj.result.n + " record(s) deleted"); db.close(); }); });