SlideShare une entreprise Scribd logo
1  sur  20
- 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();
});
});

Contenu connexe

Tendances

Tendances (20)

MongoDB
MongoDBMongoDB
MongoDB
 
Mongo db
Mongo dbMongo db
Mongo db
 
Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB
 
MongoDB presentation
MongoDB presentationMongoDB presentation
MongoDB presentation
 
Mongodb Introduction
Mongodb IntroductionMongodb Introduction
Mongodb Introduction
 
Mongo DB Presentation
Mongo DB PresentationMongo DB Presentation
Mongo DB Presentation
 
Mongo db report
Mongo db reportMongo db report
Mongo db report
 
Mongo DB 102
Mongo DB 102Mongo DB 102
Mongo DB 102
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 
MongoDB basics & Introduction
MongoDB basics & IntroductionMongoDB basics & Introduction
MongoDB basics & Introduction
 
MongoDB DOC v1.5
MongoDB DOC v1.5MongoDB DOC v1.5
MongoDB DOC v1.5
 
NOSQL and MongoDB Database
NOSQL and MongoDB DatabaseNOSQL and MongoDB Database
NOSQL and MongoDB Database
 
MongoDB for Beginners
MongoDB for BeginnersMongoDB for Beginners
MongoDB for Beginners
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
CMS Mongo DB
CMS Mongo DBCMS Mongo DB
CMS Mongo DB
 
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
 
Mongo db dhruba
Mongo db dhrubaMongo db dhruba
Mongo db dhruba
 
Mongo db
Mongo dbMongo db
Mongo db
 
OVERVIEW OF MONGODB | CREATING USER IN MONGODB & ASSIGNING ROLES
OVERVIEW OF MONGODB | CREATING USER IN MONGODB & ASSIGNING ROLES OVERVIEW OF MONGODB | CREATING USER IN MONGODB & ASSIGNING ROLES
OVERVIEW OF MONGODB | CREATING USER IN MONGODB & ASSIGNING ROLES
 

Similaire à Mongo DB

mongodb11 (1) (1).pptx
mongodb11 (1) (1).pptxmongodb11 (1) (1).pptx
mongodb11 (1) (1).pptx
RoopaR36
 
3-Mongodb and Mapreduce Programming.pdf
3-Mongodb and Mapreduce Programming.pdf3-Mongodb and Mapreduce Programming.pdf
3-Mongodb and Mapreduce Programming.pdf
MarianJRuben
 
Everything You Need to Know About MongoDB Development.pptx
Everything You Need to Know About MongoDB Development.pptxEverything You Need to Know About MongoDB Development.pptx
Everything You Need to Know About MongoDB Development.pptx
75waytechnologies
 

Similaire à Mongo DB (20)

Mongo db
Mongo dbMongo db
Mongo db
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introduction
 
Mongodb By Vipin
Mongodb By VipinMongodb By Vipin
Mongodb By Vipin
 
mongodb11 (1) (1).pptx
mongodb11 (1) (1).pptxmongodb11 (1) (1).pptx
mongodb11 (1) (1).pptx
 
MongoDB
MongoDBMongoDB
MongoDB
 
Top MongoDB interview Questions and Answers
Top MongoDB interview Questions and AnswersTop MongoDB interview Questions and Answers
Top MongoDB interview Questions and Answers
 
Mongo learning series
Mongo learning series Mongo learning series
Mongo learning series
 
MongoDB
MongoDBMongoDB
MongoDB
 
Introduction to MongoDB.pptx
Introduction to MongoDB.pptxIntroduction to MongoDB.pptx
Introduction to MongoDB.pptx
 
3-Mongodb and Mapreduce Programming.pdf
3-Mongodb and Mapreduce Programming.pdf3-Mongodb and Mapreduce Programming.pdf
3-Mongodb and Mapreduce Programming.pdf
 
Mongodb Introduction
Mongodb Introduction Mongodb Introduction
Mongodb Introduction
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Mongo db
Mongo dbMongo db
Mongo db
 
Beginner's guide to Mongodb and NoSQL
Beginner's guide to Mongodb and NoSQL  Beginner's guide to Mongodb and NoSQL
Beginner's guide to Mongodb and NoSQL
 
Everything You Need to Know About MongoDB Development.pptx
Everything You Need to Know About MongoDB Development.pptxEverything You Need to Know About MongoDB Development.pptx
Everything You Need to Know About MongoDB Development.pptx
 
The emerging world of mongo db csp
The emerging world of mongo db   cspThe emerging world of mongo db   csp
The emerging world of mongo db csp
 
A Study on Mongodb Database.pdf
A Study on Mongodb Database.pdfA Study on Mongodb Database.pdf
A Study on Mongodb Database.pdf
 
A Study on Mongodb Database
A Study on Mongodb DatabaseA Study on Mongodb Database
A Study on Mongodb Database
 
Mongodb - NoSql Database
Mongodb - NoSql DatabaseMongodb - NoSql Database
Mongodb - NoSql Database
 
UNIT-1 MongoDB.pptx
UNIT-1 MongoDB.pptxUNIT-1 MongoDB.pptx
UNIT-1 MongoDB.pptx
 

Dernier

Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
shivangimorya083
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
amitlee9823
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
MarinCaroMartnezBerg
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
amitlee9823
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
shivangimorya083
 

Dernier (20)

Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
Best VIP Call Girls Noida Sector 22 Call Me: 8448380779
 
Carero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptxCarero dropshipping via API with DroFx.pptx
Carero dropshipping via API with DroFx.pptx
 
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% SecureCall me @ 9892124323  Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
Call me @ 9892124323 Cheap Rate Call Girls in Vashi with Real Photo 100% Secure
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Midocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFxMidocean dropshipping via API with DroFx
Midocean dropshipping via API with DroFx
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
FESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdfFESE Capital Markets Fact Sheet 2024 Q1.pdf
FESE Capital Markets Fact Sheet 2024 Q1.pdf
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
Best VIP Call Girls Noida Sector 39 Call Me: 8448380779
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...Vip Model  Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
Vip Model Call Girls (Delhi) Karol Bagh 9711199171✔️Body to body massage wit...
 

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(); }); });