SlideShare a Scribd company logo
MongoDB
(No SQL)
1Dhruba Mandal
Dhruba Mandal
Email : dhruvmandal@gmail.com
Introduction To MongoDB
MongoDB is an open-source, cross-platform, document-oriented database
written in C++ that provides high performance, high availability, and automatic
scaling.
It is developed and supported by a company named 10gen
MongoDB is available under General Public license for free, and it is also available under
Commercial license from the manufacturer.
2Dhruba Mandal
Document oriented database
MongoDB stores data as documents, so it is known as document oriented database
FirstName = "Dhruba",
Address = "New Delhi",
Spouse = [{Name: "abcde"}].
FirstName ="mandal",
Address = "saket"
There are two different documents (separated by ".")
Storing data in this manner is called as document oriented database.
Example : -
3Dhruba Mandal
Purpose of building MongoDB
All the modern applications require big data, fast features development, flexible
deployment, and the older database systems not competent enough, so the MongoDB
was needed.
The primary purpose of building MongoDB is:
• Scalability : Mongo Db has Scaling capacity from single server deployments to large,
complex multi-site architectures
• Performance
• High Availability
4Dhruba Mandal
Features of MongoDB
5Dhruba Mandal
1. Support ad hoc queries : An ad hoc query means query created to obtain information
as the need arises. In MongoDB, you can search by field, range query and it also
supports regular expression searches.
2. Indexing : We can index any field in a document. Indexes are created to improve the
performance of searches. The good thing is that any field in a MongoDB document can
be indexed with primary and secondary indices.
It enables the database engine to efficiently resolve queries which make it one of the
best key features of MongoDB. The database engine can use a predefined index, which
maps documents fields and can tell the engine which documents are compatible with
this query statement, hence improves performance.
3. Replication : MongoDB supports Master Slave replication , A master can perform Reads
and Writes and a Slave copies data from the master and can only be used for reads or
back up (not writes)
4. Duplication of data : MongoDB can run over multiple servers. The data is duplicated to
keep the system up and also keep its running condition in case of hardware failure.
Dhruba Mandal 6
5. Load balancing : It has an automatic load balancing configuration because of data
placed in shards.
Note : (Sharding is a type of database partitioning that separates very large databases
the into smaller, faster, more easily managed parts called data shards)
6 . Supports map reduce and aggregation tools.
Map reduce : - Map-reduce is a data processing paradigm (model) for condensing large
volumes of data into useful aggregated results. For map-reduce operations, MongoDB
provides the mapReduce database command. which collects and condenses(i.e make
more concentrated) the aggregated data.
Map() procedure performs filtering and sorting
Reduce() procedure performs a summary operation
db.collection.mapReduce()
Aggregation tools : Aggregations operations process data records and return computed
results. Aggregation operations group values from multiple documents together, and can
perform a variety of operations on the grouped data to return a single result.
Dhruba Mandal 7
9. Provides high performance : It uses BSON instead of JSON for storing data. BSON is
binary JSON. Being Binary it improves processing efficiency of the MongoDB
10. Stores files of any size easily without complicating your stack.
11 . Easy to administer in the case of failures.
12 . It also supports:
✓ JSON data model with dynamic schemas
✓ Auto-sharding for horizontal scalability
✓ Built in replication for high availability
8. It is a schema-less database written in C++ : MongoDB is a document database in
which one collection holds different documents. Number of fields, content and size of
the document can differ from one document to another.
7. Uses JavaScript instead of Procedures
MongoDB Known as NoSQL
Dhruba Mandal 8
We know that MongoDB is a NoSQL Database .
Generally Data Base are of 3 types
1. RDBMS (Relational Database Management System)
2. OLAP (Online Analytical Processing)
3. NoSQL (NON SQL )
NoSQL Database is used to refer a non-SQL or non relational database as It provides a
mechanism for storage and retrieval of data other than tabular relations model used in
relational databases .
It is generally used to store big data and real-time web applications.
concept of collection and document
Dhruba Mandal 9
Database :
• Database is a physical container for collections.
• Each database gets its own set of files on the file system.
• A single MongoDB server typically has multiple databases.
Collection
• Collection is a group of MongoDB documents.
• It is the equivalent of an RDBMS table .
• A collection exists within a single database
• Collections do not enforce a schema.
• Documents within a collection can have different fields
Document :
• A document is a set of key-value pairs.
• Documents have dynamic schema.
• Dynamic schema means that documents in the same collection do not need to have
the same set of fields or structure, and common fields in a collection's documents
may hold different types of data
Differences in Terminology
Dhruba Mandal 10
RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Table Join Embedded Documents
Primary Key
Primary Key (Default key _id provided by mongodb
itself)
Mongo DB Vs MySQL
Dhruba Mandal 11
MongoDB MySQL
MongoDB is an open-source database developed
by MongoDB, Inc
MySQL is a popular open-source relational
database management system (RDBMS) that is
developed by oracle Corporation
No Schema definition required Structured data with clear schema
In MongoDB, data represents in a collection of
JSON documents
In MySQL, data is in tables and rows
In MongoDB, each individual records are stored as
‘documents’.
In MySQL, each individual records are stored as
‘rows’ in a table.
MongoDB’s querying is object-oriented, which
means you pass MongoDB a document explaining
what you are querying
The query language is called Structured Query
Language i.e. you have to put a string in the query
language that the DB system parses
Mongo DB doesn’t support joints, it supports
multi-dimensional data types like other
documents and arrays.
MySQL support join operation
In MongoDB, if an index is not found, every
document within a collection must be scanned to
select the documents that provide a match to the
query statement
With MySQL, if an index is not defined, the
database engine must scan the entire table to find
all relevant rows.
MongoDB Advantages
Dhruba Mandal 12
❖ MongoDB is schema less. It is a document database in which one collection holds
different documents.
❖ There may be difference between number of fields, content and size of the
document from one to other.
❖ Structure of a single object is clear in MongoDB.
❖ There are no complex joins in MongoDB.
❖ MongoDB provides the facility of deep query because it supports a powerful dynamic
query on documents.
❖ It is very easy to scale.
❖ It uses internal memory for storing working sets and this is the reason of its fast
access.
MongoDB Data types
Dhruba Mandal 13
Data Types Description
Arrays This data type is used to store a list or multiple values into a single key.
Boolean This data type is used to store Boolean values. It just shows YES/NO values.
Date
This data type stores the current date or time in Unix time format. It makes
you Possible to specify your own date time by creating object of date and
pass the value of date, month, year into it.
Double Double data type stores floating point values.
Integer
Integer is used to store the numeric value. It can be 32 bit or 64 bit
depending on the server you are using.
Min/Max
Keys
This data type compare a value against the lowest and highest bson
elements.
Null It is used to store null values.
Object Object data type is used for embedded documents.
String
String is the most commonly used data type. A string must be UTF 8 valid in
mongodb.
Symbol It is generally used for languages that use a specific type.
Installing MongoDB
Dhruba Mandal 14
Step 1 – Adding the MongoDB Repository
sudo vi /etc/yum.repos.d/mongodb-org.repo
2. Inside mongodb-org.repo write the below configuration
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongod
b-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
3. Save and Close File by !wq
4. Run command yum repolist
The above repolist command displays a list of enabled repositories:
Dhruba Mandal 15
5. Now installing MongoDB run below command
sudo yum install mongodb-org
There are two Is this ok [y/N]: prompts press Y
6. Next, start the MongoDB service with the systemctl utility:
sudo systemctl reload mongod
sudo systemctl start mongod
7 . check that the service started by viewing the end of the mongod.log file with
the tail command:
sudo tail /var/log/mongodb/mongod.log
. . .
[initandlisten] waiting for connections on port 27017
An output of waiting for a connection confirms that MongoDB has started successfully
and we can access the database server with the MongoDB Shell:
Output
Dhruba Mandal 16
8. To login into mongo db Type command
mongo
Note: When you launched the MongoDB Shell you may have seen a warning like this:
** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of
processes should be at least 32000 : 0.5 times number of files.
9. To resolve the warning, alter the processes soft rlimit value for mongod by editing
the 20-nproc.conf file:
sudo vi /etc/security/limits.d/20-nproc.conf
10. Add the following line to the end of file :
mongod soft nproc 32000
PTO
Dhruba Mandal 17
11 . For the new limit to be available to MongoDB, restart it using the systemctl utility:
sudo systemctl restart mongod
12 . Leave the mongod process running in the background, but quit the shell with
the exit command:
> exit
13 . Use the systemctl utility to check its startup status
systemctl is-enabled mongod; echo $?
Output
. . . enabled
0
An output of zero confirms an enabled daemon, which we want
14 . In the event of a disabled daemon, use the systemctl utility to enable it :
sudo systemctl enable mongod
Secure MongoDB with username and password
Dhruba Mandal 18
1. Connect to the instance.
mongo
2. At the mongo command line, set the administrator:
use admin
db.createUser(
{
user: "admin",
pwd: "admin123",
roles: [ { role: "root", db: "admin" }]
}
)exit;
3. Authenticate user with below command
db.auth("admin", "password")
Exit the mongo shell, re-connect, authenticate as the user.
4. Access the MongoDB with below command
mongo -u admin -p myadminpassword 127.0.0.1/admin
Accessing MongoDB Remotely
Dhruba Mandal 19
Once MongoDB credentials is created and Authenticated it is now time to enable
MongoDB to start using these access controls To do so .
1. open the file mongod.conf by below command
sudo vim /etc/mongod.conf
2. In the file at last add the below line
security:
authorization: enabled
3. Also in same file change bind IP from 127.0.0.1 to 0.0.0.0 for all or you can
give the IP of servers you want . 0.0.0.0. means for all
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 #default value is 127.0.0.1
4. Restart the MongoDB using below command
sudo service mongod restart
Dhruba Mandal 20
5. On client machine or machine from which we want to access MongoDB Install the
MongoDB- Client .
Steps :
1. Create the repository by command
sudo vim/etc/yum.repos.d/mongodb.repo
2. Inside mongodb-org.repo write the below configuration
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodborg/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
3. Update the repository index by below command
sudo yum update -y
Dhruba Mandal 21
4. Install the MongoDB Shell Client with below command
sudo yum install mongodb-org-shell -y
5. Now you can connect your mongo DB from client or remote machine by below
command
mongo -u <username> -p <password> <ip-address>:27017/<collection-name>
Example : In my case I am connecting my MongoDB on my Centos 7 machine
10.0.205.53 from my centos7 machine 10.0.205.50 : Output is as below :
Concepts of Data Modeling
Dhruba Mandal 22
• Data modeling is a representation of the data structures in a table for a company’s
database
• It is a very powerful expression of the company's business requirements.
• Data models are used for many purposes, from high-level conceptual models to physical
data models.
Three basic styles of data modeling are generally used in practice today.
a. Conceptual Data Models(CDMs): High-level, static business structures and concepts
b. Logical Data Models (LDMs): Entity types, data attributes and relationships between
entities
c. Physical Data Models (PDMs): The internal schema database design
Data Modeling in MongoDB
Dhruba Mandal 23
➢ Data in MongoDB has a flexible schema. documents in the same collection
➢ The main challenge in data modeling is balancing the need of the application, the
performance characteristics of the database engine, and the data retrieval patterns
Below are some considerations while designing Schema in MongoDB
✓Always design schema according to user requirements.
✓Combine objects into one document if you will use them together. Otherwise separate
them (but make sure there should not be need of joins)
✓Do join on write operations not on read operations.
✓Optimize your schema for most frequent use cases.
✓Do complex aggregation in the schema.
✓ Duplicate the data (but limited) because disk space is cheap as compare to compute
time
Database and It’s Command in MongoDB
Dhruba Mandal 24
Database is a physical container for collections , each database gets its own set of
files on the file system. A single MongoDB server typically has multiple databases
1. To create database use command : use Database_name
Note : (use database name will create the database if database already exist
it will switch to that database)
2. Switching to other database use command : use Database_name
3. To check your currently selected database, use the command : db
Note: (It will display your present DB)
4. to check your databases list, use the command : show dbs
5. To delete the database in MongoDB
If you want to delete the database
1.First go to that database by command : use Database_name then
2.use command db.dropDatabase()
Collection & It's Command in MongoDB
Dhruba Mandal 25
Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table .
A collection exists within a single database it does not enforce schema so the
document with in collection can have different fields.
1. command to create collection
. Go to DB use admin and run below command
db.createCollection(“name”, “options”)
Parameter Type Description
Name String Name of the collection to be
created
Options Document (Optional) Specify options
about memory size and
indexing
Creating collection by name
mycollection
Dhruba Mandal 26
2. After creating Collection to see the collection use command
show collections
command show collection will show all the collection as below
If we are getting any error like
"errmsg" : "not authorized on DB_Name to execute command { create: "mycollection"
}",
Means the user is not authorized and in this case we need to authorize user by below
command
use admin; db.grantRolesToUser('admin', [{ role: 'root', db: 'admin' }])
Here in example user is admin but for any user we can do the same
Important Note
Dhruba Mandal 27
Optional parameter of collections
Field Type Description
capped Boolean
(Optional) If true, enables a capped collection. Capped collection is a fixed
size collection that automatically overwrites its oldest entries when it reaches
its maximum size. ( If you specify true, you need to specify size parameter
also.)
autoIndexId Boolean
(Optional) If true, automatically create index on _id fields Default value is
false.
size number
(Optional) Specifies a maximum size in bytes for a capped collection. If capped
is true, then you need to specify this field also.
max number
(Optional) Specifies the maximum number of documents allowed in the
capped collection.
Example below shows the syntax of createCollection() method with few important options
db.createCollection("mycol", { capped : true, autoIndexId : true, size : 6142800, max : 10000 } )
{ "ok" : 1 } >
Dhruba Mandal 28
2. Command to drop Collection
In MongoDB, db.collection.drop() method is used to drop a collection from a
database.
It completely removes a collection from the database and does not leave any
indexes associated with the dropped collections.
Basic syntax : db.COLLECTION_NAME.drop()
First check the already existing collections in your database. By command
use DB_name (name of the db from where u want to drop collection)
show collections
db.mycollection.drop() (mycollection = Name of collection you want to drop)
If successful it will give output as : true
Documents and It’s Command in MongoDB
Dhruba Mandal 29
A document is a set of key-value pairs. It has have dynamic schema , it means that
documents in the same collection do not need to have the same set of fields or
structure, and common fields in a collection's documents may hold different types
of data.
MongoDB insert documents : There are also two methods "db.collection.update()"
method and "db.collection.save()" method used for adding or inserting new documents
into a collection in your database
Syntax : The basic syntax of insert() command is as follows −
db.COLLECTION_NAME.insert(document)
> show collections
users
> db.users.insert({
...
... id: '1' ,
... loginId: 'mandal',
... userPass: 'mandal',
... userType: 'admin',
... active: '1',
...
... })
Inserting Multiple record
Dhruba Mandal 30
db.users.insert([
{id: '4' ,
loginId: 'rahul',
userPass: 'rahul',
userType: 'admin',
active: '1',
},
{
id: '5' ,
loginId: 'mohit',
userPass: 'mohit',
userType: 'admin',
active: '1',
}
])
Here users is
collection name
Also insert ([ at
beginning and
write
documents
inside { } for
other use ,
MongoDB - Query Document
Dhruba Mandal 31
To query data from MongoDB collection, you need to use MongoDB's find() method.
Syntax
The basic syntax of find() method is as follows −
>db.COLLECTION_NAME.find()
db.users.find() (In my case users is collection name)
To display the results in a formatted way, you can use pretty() method. (same as using
G in MySQL )
>db.COLLECTION_NAME.find().pretty()
>db.users.find({"loginId":"mandal"}) (like where condition in MySQL )
RDBMS Where Clause Equivalents in MongoDB
Dhruba Mandal 32
To query the document on the basis of some condition, you can use following
operations
Operation Syntax Example RDBMS Equivalent
Equality {<key>:<value>} db.mycol.find({"by":"mandal"}).pretty() where by = 'mandal'
Less Than {<key>:{$lt:<value>}} db.mycol.find({"likes":{$lt:50}}).pretty() where likes < 50
Less Than Equals {<key>:{$lte:<value>}} db.mycol.find({"likes":{$lte:50}}).pretty() where likes <= 50
Greater Than {<key>:{$gt:<value>}} db.mycol.find({"likes":{$gt:50}}).pretty() where likes > 50
Greater Than Equals {<key>:{$gte:<value>}} db.mycol.find({"likes":{$gte:50}}).pretty() where likes >= 50
Not Equals {<key>:{$ne:<value>}} db.mycol.find({"likes":{$ne:50}}).pretty() where likes != 50
AND in MongoDB
Dhruba Mandal 33
In the find() method, if you pass multiple keys by separating them by ',' then
MongoDB treats it as AND condition. Following is the basic syntax of AND −
>db.collectionname.find(
{ $and: [
{"id“: “1”}, {key2:value2}
]
}
).pretty()
OR in MongoDB
Dhruba Mandal 34
To query documents based on the OR condition, you need to use $or keyword.
Following is the basic syntax of OR −
>db.collectionname.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
We can also use AND and OR Together
db.mycol.find({"likes": {$gt:10}, $or: [{"key1: value1"}, {"title": "key2:
value2"}]}).pretty()
MongoDB - Projection
Dhruba Mandal 35
In MongoDB, projection means selecting only the necessary data rather than
selecting whole of the data of a document. If a document has 5 fields and you need
to show only 3, then select only 3 fields from them.
The basic syntax of find() method with projection is as follows −
>db.COLLECTION_NAME.find({},{KEY:1})
Example :
> db.users.find({},{"loginId":1,_id:0})
{ "loginId" : "Sumit" }
{ "loginId" : "harsh" }
{ "loginId" : "one97" }
{ "loginId" : "mohit" }
Please note _id field is always displayed while executing find() method, if you don't
want this field, then you need to set it as 0.
MongoDB - Update Document
Dhruba Mandal 36
MongoDB's update() and save() methods are used to update document into a
collection. The update() method updates the values in the existing document while
the save() method replaces the existing document with the document passed in
save() method.
MongoDB Update() Method
Syntax
>db.COLLECTION_NAME.update(SELECTION_CRITERIA,UPDATED_DATA)
example
> db.users.update({"loginId" : "rahul"},{$set:{"loginId" : "one97"}})
Result : WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.users.find() will display the changes
Dhruba Mandal 37
MongoDB Save() Method
The save() method replaces the existing document with the new document passed in
the save() method.
Syntax
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
db.users.save(
{
"_id" : ObjectId("5d6ca5b13212ff16c9e6ef1a"), "userPass":"mandal",
"by":"mandal@123"
}
)
Update is better to use than save
MongoDB - Delete Document
Dhruba Mandal 38
MongoDB's remove() method is used to remove a document from the collection.
remove() method accepts two parameters. One is deletion criteria and second is just One
flag.
deletion criteria − (Optional) deletion criteria according to documents will be removed.
justOne − (Optional) if set to true or 1, then remove only one document.
Basic syntax of remove()
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
db.users.remove({ "userPass" : "mandal"})
Example
Dhruba Mandal 39
If there are multiple records and you want to delete only the first record, then set
justOne parameter in remove() method.
db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
(Note : Here 1 means only first which meets deletion criteria , if multiple
rows meet the same criteria only first row will be deleted )
Remove All Documents of that collection
>db.collectionmane.remove({})
(Note : This is equivalent of SQL's truncate command )
The Limit() and skip() Method
Dhruba Mandal 40
To limit the records in MongoDB, you need to use limit() method.
The basic syntax of limit() method is as follows −
>db.COLLECTION_NAME.find().limit(NUMBER)
> db.users.find().limit(2) will only display 2 rows
Apart from limit() method, there is one more method skip() which also accepts
number type argument and is used to skip the number of documents.
The basic syntax of skip() method is as follows
db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
> db.users.find().limit(2).skip(1)
i.e. it will skip the number of rows and will show 2 value after that row
MongoDB - Sort Records
Dhruba Mandal 41
In MongoDB, sort() method is used to sort the documents in the collection.
This method accepts a document containing list of fields along with their sorting
order.
The sorting order is specified as 1 or -1.
1 is used for ascending order sorting.
-1 is used for descending order sorting.
Basic syntax for sort in as below
>db.COLLECTION_NAME.find().sort({KEY:1})
Example :
>db.users.find({},{"loginId":1,_id:0}).sort({"loginId" :-1})
>db.users.find({},{"loginId":1,_id:0}).sort({"loginId" : 1})
MongoDB - Indexing
Dhruba Mandal 42
Indexes are special data structures, that store a small portion of the data set in an easy-
to-traverse form.
The index stores the value of a specific field or set of fields, ordered by the value of the
field as specified in the index
To create an index you need to use ensureIndex() method of MongoDB.
Syntax
>db.COLLECTION_NAME.ensureIndex({KEY:1})
Here key is the name of the field on which you want to create index and 1 is for ascending
order-1 create index in descending order
db.users.ensureIndex({"loginId":1})
By using ensureIndex() method you can pass multiple fields, to create index on
multiple fields.
Syntax
db.mycol.ensureIndex({"title":1,"description":-1})
db.users.ensureIndex({"loginId":1,"id" :1 })
MongoDB - Aggregation
Dhruba Mandal 43
Aggregations operations process data records and return computed results
In SQL count(*) and with group by is an equivalent of MongoDB aggregation.
For the aggregation in MongoDB, you should use aggregate() method.
Basic syntax of aggregate() method is as follows −
db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Example :
db.users.aggregate([{$group : {_id : "loginId", num_tutorial : {$sum : 1}}}])
Output :
{ "_id" : "by_loginId", "num_tutorial" : 4 }
Dhruba Mandal 44
list of available aggregation expressions.
Expression Description Example
$sum
Sums up the defined value from all documents in the
collection.
db.users.aggregate([{$group : {_id : "$by_user", num_tutorial :
{$sum : "$likes"}}}])
$avg
Calculates the average of all given values from all
documents in the collection.
db.users.aggregate([{$group : {_id : "$by_user", num_tutorial :
{$avg : "$likes"}}}])
$min
Gets the minimum of the corresponding values from all
documents in the collection.
db.users.aggregate([{$group : {_id : "$by_user", num_tutorial :
{$min : "$likes"}}}])
$max
Gets the maximum of the corresponding values from all
documents in the collection.
db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial :
{$max : "$likes"}}}])
$push Inserts the value to an array in the resulting document.
db.users.aggregate([{$group : {_id : "$by_user", url : {$push:
"$url"}}}])
$addToSet
Inserts the value to an array in the resulting document
but does not create duplicates.
db.users.aggregate([{$group : {_id : "$by_user", url :
{$addToSet : "$url"}}}])
$first
Gets the first document from the source documents
according to the grouping. Typically this makes only
sense together with some previously applied “$sort”-
stage.
db.users.aggregate([{$group : {_id : "$by_user", first_url : {$first
: "$url"}}}])
$last
Gets the last document from the source documents
according to the grouping. Typically this makes only
sense together with some previously applied “$sort”-
stage.
db.users.aggregate([{$group : {_id : "$by_user", last_url : {$last
: "$url"}}}])
MongoDB - Replication
Dhruba Mandal 45
Replication is the process of synchronizing data across multiple servers. It provides
redundancy and increases data availability with multiple copies of data on different
database servers
1. Issue the below command to generate your key file:
openssl rand -base64 756 > mongo-keyfile
2. Create the /opt/mongo directory to store your key file:
sudo mkdir /opt/mongo
3. Move your key file to /opt/mongo and assign it the correct permissions:
sudo mv ~/mongo-keyfile /opt/mongo
sudo chmod 400 /opt/mongo/mongo-keyfile
4. Update the ownership of your key
sudo chown mongod:mongod /opt/mongo/mongo-keyfile
5. Create an Administrative User on server which u choose as primary member of
your replication set
Dhruba Mandal 46
Create an Administrative User by below steps
5a. log in to the mongo shell by command
Mongo
5b. Connect to the admin database:
use admin
5c. Create an administrative user with root privileges. Replace “password” with a strong
password of your choice:
db.createUser({user: "mongo-admin", pwd: "password", roles:[{role: "root", db: "admin"}]})
6 . Open the configuration file of MongoDB on both server by command
sudo vim /etc/mongod.conf
7 In configuration file remove hash (#) from replication and add below lines
replSetName: "myreplica01“
( Important Note : Here myreplica01 is replication name, you can use any name but it should be same on both
server also there should be 2 space after replication: i.e. don’t write replSetName just below replication: use 2
space else it will show error )
Dhruba Mandal 47
8 . Once changes has been made restart the mongod service by command:
sudo systemctl restart mongod
9 . Connect via SSH to the node that you intend to use as your primary
mongo -u mongo-admin -p --authenticationDatabase admin
Example in my case : mongo -u admin -p admin123 127.0.0.1/admin
10 . From the mongo shell, initiate the replica set:
rs.initiate()
It will give output as some thing below
11. While still connected to the mongo shell, add the other hosts to the replica set:(any
number of set i.e. servers)
rs.add("mongod1.net:27017")
rs.add("mongod2.net:27017")
12 . Once all members have been added, check the configuration of your replica set:
rs.conf()
Do any changes on primary and it will replicate on secondary
MongoDB - Sharding
Dhruba Mandal 48
Sharding is a concept in MongoDB, which splits large data sets into small data sets
across multiple MongoDB instances.
Sometimes the data within MongoDB will be so huge, that queries against such big
data sets can cause a lot of CPU utilization on the server.
To tackle this situation, MongoDB has a concept of Sharding, which is basically the
splitting of data sets across multiple MongoDB instances.
Mongo DB Sharding uses horizontal scaling to solve the problems
Concept of Horizontal and Vertical Scaling
Horizontal scaling means that you scale by adding more machines into your pool of
resources whereas Vertical scaling means that you scale by adding more power
(CPU, RAM) to an existing machine.
How to Implement Sharding
Dhruba Mandal 49
Shards are implemented by using clusters which are nothing but a group of MongoDB
instances.
1. A Shard – This is a MongoDB instance which holds the subset of the data. In production
environments, all shards need to be part of replica sets
2. Config server – This is a MongoDB instance which holds metadata (i.e. a set of data that
describes and gives information about other data) about the cluster, basically information
about the various MongoDB instances which will hold the shard data.
3. A Router – This is a MongoDB instance which basically is responsible to re-directing
the commands send by the client to the right servers.
MongoDB Backup & Restore
Dhruba Mandal 50
All backup and restore command will run on node machine not on MongoDB
Backup Single Database
mongodump --db mydb --out /backup/db/
Ex: - mongodump --db VodaIdeapanel --out /backup/db/
(Here VodaIdeapanel is my db name and /backup/db/ is my path )
Backup All Databases
mongodump --out /backup/db/
Backup Single Collection
mongodump --collection mycollection --db mydb --out /backup/db/
Example : mongodump --collection user --db VodaIdeapanel --out /backup/db/
(Note here user is my collection name )
Remote database backup
specify host, port, username and password for remote databases connections backups like
below.
mongodump --host 10.0.1.7 --port 27017 --username admin --password some password --
db mydb --out /backup/db/
Dhruba Mandal 51
mongorestore is command line tool for restoring MongoDB database backup
Restore MongoDB Database
Dhruba Mandal 52
mongorestore is command line tool for restoring MongoDB database backup
mongorestore --db mydb --drop /backup/db/mydb
Example : mongorestore --db pannel --drop /backup/db/VodaIdeapanel
(Here /data/db/ is location of your MongoDB data directory and /backup/db is
location of backup directory.)
MongoDB Backup Shell Script
#!/bin/sh
TODAY=`date +%d%b%Y`
BACKUP_DIR=/backup/db
mkdir -p ${BACKUP_DIR}/${TODAY}
mongodump -h <DATABASE_HOST> -d <DATABASE_NAME> -u <USERNAME> -p
<PASSWRD> --out ${BACKUP_DIR}/${TODAY}/
When to use MangoDB /MySQL
Dhruba Mandal 53
MongoDB MySQL
High Availability : When you need high Availability
of data with automatic fast and instant data
recovery
Low Maintenance: When your just starting and
your database is not going to scale much, MySQL
will help you in easy and low maintenance set up
IN Built Sharding : In future, if you're going to grow
big as MangoDB has inbuilt Sharding solution
Limited Budget: if you want high performance in
limited budget
Unstable Schema: if you have an unstable schema
and you want to reduce your schema migration cost
Fixed Schema: If you have fixed schema and data
structure isn't going to change over the time like
Wikipedia
NO DBA : if you don't have Database Administrator
(required for larger)
High Transaction: If high transaction rate is going
to be your requirement (i.e. high insert or select)
Cloud Computing: If most of your services are
cloud-based , MongoDB is best suitable
Data Security: If data security is your top priority ,
MySQL is the most secure DBMS
References
Dhruba Mandal 54
https://www.tutorialsjar.com/key-features-of-mongodb/
https://www.quora.com/How-can-Mongo-db-provides-high-performance
https://www.mongodb.com/nosql-explained
https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-centos-7
https://www.javatpoint.com/mongodb-tutorial
https://stackoverflow.com/questions/4881208/how-to-secure-mongodb-with-username-
and-password
https://stackoverflow.com/questions/23943651/mongodb-admin-user-not-authorized
https://www.linode.com/docs/databases/mongodb/create-a-mongodb-replica-set/
http://www.dba86.com/docs/mongo/2.4/reference/limits.html
https://www.howtoforge.com/tutorial/deploying-mongodb-sharded-cluster-on-centos-
7/#three-sharding-components
https://tecadmin.net/backup-and-restore-mongodb-database/
Ad

More Related Content

What's hot (20)

Mongo DB
Mongo DB Mongo DB
Mongo DB
Tata Consultancy Services
 
Mongo db intro.pptx
Mongo db intro.pptxMongo db intro.pptx
Mongo db intro.pptx
JWORKS powered by Ordina
 
MongoDB
MongoDBMongoDB
MongoDB
nikhil2807
 
Mongodb basics and architecture
Mongodb basics and architectureMongodb basics and architecture
Mongodb basics and architecture
Bishal Khanal
 
Introduction to mongodb
Introduction to mongodbIntroduction to mongodb
Introduction to mongodb
neela madheswari
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
César Trigo
 
An Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDBAn Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDB
Lee Theobald
 
Mongo DB Presentation
Mongo DB PresentationMongo DB Presentation
Mongo DB Presentation
Jaya Naresh Kovela
 
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
SpringPeople
 
Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB
HabileLabs
 
MongoDB.pptx
MongoDB.pptxMongoDB.pptx
MongoDB.pptx
Sigit52
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
MongoDB
 
Oracle archi ppt
Oracle archi pptOracle archi ppt
Oracle archi ppt
Hitesh Kumar Markam
 
MongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To TransactionsMongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To Transactions
Mydbops
 
The Basics of MongoDB
The Basics of MongoDBThe Basics of MongoDB
The Basics of MongoDB
valuebound
 
Mongodb tutorial at Easylearning Guru
Mongodb tutorial  at Easylearning GuruMongodb tutorial  at Easylearning Guru
Mongodb tutorial at Easylearning Guru
KCC Software Ltd. & Easylearning.guru
 
Introducing MongoDB Atlas
Introducing MongoDB AtlasIntroducing MongoDB Atlas
Introducing MongoDB Atlas
MongoDB
 
Mongo DB: Operational Big Data Database
Mongo DB: Operational Big Data DatabaseMongo DB: Operational Big Data Database
Mongo DB: Operational Big Data Database
Xpand IT
 
Mongo Nosql CRUD Operations
Mongo Nosql CRUD OperationsMongo Nosql CRUD Operations
Mongo Nosql CRUD Operations
anujaggarwal49
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
TO THE NEW | Technology
 
Mongodb basics and architecture
Mongodb basics and architectureMongodb basics and architecture
Mongodb basics and architecture
Bishal Khanal
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
César Trigo
 
An Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDBAn Introduction To NoSQL & MongoDB
An Introduction To NoSQL & MongoDB
Lee Theobald
 
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
SpringPeople
 
Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB
HabileLabs
 
MongoDB.pptx
MongoDB.pptxMongoDB.pptx
MongoDB.pptx
Sigit52
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
MongoDB
 
MongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To TransactionsMongoDB WiredTiger Internals: Journey To Transactions
MongoDB WiredTiger Internals: Journey To Transactions
Mydbops
 
The Basics of MongoDB
The Basics of MongoDBThe Basics of MongoDB
The Basics of MongoDB
valuebound
 
Introducing MongoDB Atlas
Introducing MongoDB AtlasIntroducing MongoDB Atlas
Introducing MongoDB Atlas
MongoDB
 
Mongo DB: Operational Big Data Database
Mongo DB: Operational Big Data DatabaseMongo DB: Operational Big Data Database
Mongo DB: Operational Big Data Database
Xpand IT
 
Mongo Nosql CRUD Operations
Mongo Nosql CRUD OperationsMongo Nosql CRUD Operations
Mongo Nosql CRUD Operations
anujaggarwal49
 

Similar to Mongo db dhruba (20)

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
 
Top MongoDB interview Questions and Answers
Top MongoDB interview Questions and AnswersTop MongoDB interview Questions and Answers
Top MongoDB interview Questions and Answers
jeetendra mandal
 
Mongodb
MongodbMongodb
Mongodb
ASEEMSRIVASTAVA22
 
05201349_Unit_7_FSWD_ advanced learning.pptx
05201349_Unit_7_FSWD_ advanced learning.pptx05201349_Unit_7_FSWD_ advanced learning.pptx
05201349_Unit_7_FSWD_ advanced learning.pptx
ozakamal8
 
05201349_Unit_7_FSWD_II(1) with advance.pptx
05201349_Unit_7_FSWD_II(1) with advance.pptx05201349_Unit_7_FSWD_II(1) with advance.pptx
05201349_Unit_7_FSWD_II(1) with advance.pptx
ozakamal8
 
05201349_Unit_7_FSWD_ advanced learning.pptx
05201349_Unit_7_FSWD_ advanced learning.pptx05201349_Unit_7_FSWD_ advanced learning.pptx
05201349_Unit_7_FSWD_ advanced learning.pptx
ozakamal8
 
05201349_Unit_7_FSWD_II(1) with advance.pptx
05201349_Unit_7_FSWD_II(1) with advance.pptx05201349_Unit_7_FSWD_II(1) with advance.pptx
05201349_Unit_7_FSWD_II(1) with advance.pptx
ozakamal8
 
Mongo db
Mongo dbMongo db
Mongo db
Gyanendra Yadav
 
A Study on Mongodb Database.pdf
A Study on Mongodb Database.pdfA Study on Mongodb Database.pdf
A Study on Mongodb Database.pdf
Jessica Navarro
 
A Study on Mongodb Database
A Study on Mongodb DatabaseA Study on Mongodb Database
A Study on Mongodb Database
IJSRD
 
mongodb11 (1) (1).pptx
mongodb11 (1) (1).pptxmongodb11 (1) (1).pptx
mongodb11 (1) (1).pptx
RoopaR36
 
MongoDB.pptx
MongoDB.pptxMongoDB.pptx
MongoDB.pptx
Aayush Chimaniya
 
Mongodb By Vipin
Mongodb By VipinMongodb By Vipin
Mongodb By Vipin
Vipin Mundayad
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introduction
sethfloydjr
 
how_can_businesses_address_storage_issues_using_mongodb.pdf
how_can_businesses_address_storage_issues_using_mongodb.pdfhow_can_businesses_address_storage_issues_using_mongodb.pdf
how_can_businesses_address_storage_issues_using_mongodb.pdf
sarah david
 
how_can_businesses_address_storage_issues_using_mongodb.pptx
how_can_businesses_address_storage_issues_using_mongodb.pptxhow_can_businesses_address_storage_issues_using_mongodb.pptx
how_can_businesses_address_storage_issues_using_mongodb.pptx
sarah david
 
What are the major components of MongoDB and the major tools used in it.docx
What are the major components of MongoDB and the major tools used in it.docxWhat are the major components of MongoDB and the major tools used in it.docx
What are the major components of MongoDB and the major tools used in it.docx
Technogeeks
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introduction
dinkar thakur
 
Node Js, AngularJs and Express Js Tutorial
Node Js, AngularJs and Express Js TutorialNode Js, AngularJs and Express Js Tutorial
Node Js, AngularJs and Express Js Tutorial
PHP Support
 
MongoDB Interview Questions PDF By ScholarHat
MongoDB Interview Questions PDF By ScholarHatMongoDB Interview Questions PDF By ScholarHat
MongoDB Interview Questions PDF By ScholarHat
Scholarhat
 
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
 
Top MongoDB interview Questions and Answers
Top MongoDB interview Questions and AnswersTop MongoDB interview Questions and Answers
Top MongoDB interview Questions and Answers
jeetendra mandal
 
05201349_Unit_7_FSWD_ advanced learning.pptx
05201349_Unit_7_FSWD_ advanced learning.pptx05201349_Unit_7_FSWD_ advanced learning.pptx
05201349_Unit_7_FSWD_ advanced learning.pptx
ozakamal8
 
05201349_Unit_7_FSWD_II(1) with advance.pptx
05201349_Unit_7_FSWD_II(1) with advance.pptx05201349_Unit_7_FSWD_II(1) with advance.pptx
05201349_Unit_7_FSWD_II(1) with advance.pptx
ozakamal8
 
05201349_Unit_7_FSWD_ advanced learning.pptx
05201349_Unit_7_FSWD_ advanced learning.pptx05201349_Unit_7_FSWD_ advanced learning.pptx
05201349_Unit_7_FSWD_ advanced learning.pptx
ozakamal8
 
05201349_Unit_7_FSWD_II(1) with advance.pptx
05201349_Unit_7_FSWD_II(1) with advance.pptx05201349_Unit_7_FSWD_II(1) with advance.pptx
05201349_Unit_7_FSWD_II(1) with advance.pptx
ozakamal8
 
A Study on Mongodb Database.pdf
A Study on Mongodb Database.pdfA Study on Mongodb Database.pdf
A Study on Mongodb Database.pdf
Jessica Navarro
 
A Study on Mongodb Database
A Study on Mongodb DatabaseA Study on Mongodb Database
A Study on Mongodb Database
IJSRD
 
mongodb11 (1) (1).pptx
mongodb11 (1) (1).pptxmongodb11 (1) (1).pptx
mongodb11 (1) (1).pptx
RoopaR36
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introduction
sethfloydjr
 
how_can_businesses_address_storage_issues_using_mongodb.pdf
how_can_businesses_address_storage_issues_using_mongodb.pdfhow_can_businesses_address_storage_issues_using_mongodb.pdf
how_can_businesses_address_storage_issues_using_mongodb.pdf
sarah david
 
how_can_businesses_address_storage_issues_using_mongodb.pptx
how_can_businesses_address_storage_issues_using_mongodb.pptxhow_can_businesses_address_storage_issues_using_mongodb.pptx
how_can_businesses_address_storage_issues_using_mongodb.pptx
sarah david
 
What are the major components of MongoDB and the major tools used in it.docx
What are the major components of MongoDB and the major tools used in it.docxWhat are the major components of MongoDB and the major tools used in it.docx
What are the major components of MongoDB and the major tools used in it.docx
Technogeeks
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introduction
dinkar thakur
 
Node Js, AngularJs and Express Js Tutorial
Node Js, AngularJs and Express Js TutorialNode Js, AngularJs and Express Js Tutorial
Node Js, AngularJs and Express Js Tutorial
PHP Support
 
MongoDB Interview Questions PDF By ScholarHat
MongoDB Interview Questions PDF By ScholarHatMongoDB Interview Questions PDF By ScholarHat
MongoDB Interview Questions PDF By ScholarHat
Scholarhat
 
Ad

More from Dhrubaji Mandal ♛ (15)

Nginx
NginxNginx
Nginx
Dhrubaji Mandal ♛
 
Nginx dhruba mandal
Nginx dhruba mandalNginx dhruba mandal
Nginx dhruba mandal
Dhrubaji Mandal ♛
 
Cloud Monitoring tool Grafana
Cloud Monitoring  tool Grafana Cloud Monitoring  tool Grafana
Cloud Monitoring tool Grafana
Dhrubaji Mandal ♛
 
Telecommunication
TelecommunicationTelecommunication
Telecommunication
Dhrubaji Mandal ♛
 
Signaling system 7
Signaling system 7 Signaling system 7
Signaling system 7
Dhrubaji Mandal ♛
 
Dessertation project on BPM in IT Industry
Dessertation project  on BPM in IT Industry Dessertation project  on BPM in IT Industry
Dessertation project on BPM in IT Industry
Dhrubaji Mandal ♛
 
Business Process Management in IT company
Business Process Management  in IT company Business Process Management  in IT company
Business Process Management in IT company
Dhrubaji Mandal ♛
 
SERVICES MANAGEMENT IN HOTEL INDUSTRY
SERVICES  MANAGEMENT IN HOTEL INDUSTRY SERVICES  MANAGEMENT IN HOTEL INDUSTRY
SERVICES MANAGEMENT IN HOTEL INDUSTRY
Dhrubaji Mandal ♛
 
Supply chain presentation (Mumbai Dabba wala)
Supply chain presentation (Mumbai Dabba wala)Supply chain presentation (Mumbai Dabba wala)
Supply chain presentation (Mumbai Dabba wala)
Dhrubaji Mandal ♛
 
Information technology implementation in power distribution
Information technology implementation in  power distributionInformation technology implementation in  power distribution
Information technology implementation in power distribution
Dhrubaji Mandal ♛
 
Project report on exploring express cargo
Project report on exploring express cargoProject report on exploring express cargo
Project report on exploring express cargo
Dhrubaji Mandal ♛
 
Presentation on job specification
Presentation  on  job specificationPresentation  on  job specification
Presentation on job specification
Dhrubaji Mandal ♛
 
Project Report On Emotion At Work Place -- Dhrubaji Mandal
Project Report On Emotion At Work Place  -- Dhrubaji Mandal Project Report On Emotion At Work Place  -- Dhrubaji Mandal
Project Report On Emotion At Work Place -- Dhrubaji Mandal
Dhrubaji Mandal ♛
 
Dividend policy report
Dividend policy reportDividend policy report
Dividend policy report
Dhrubaji Mandal ♛
 
Presentation on Russian culture
Presentation on Russian culturePresentation on Russian culture
Presentation on Russian culture
Dhrubaji Mandal ♛
 
Dessertation project on BPM in IT Industry
Dessertation project  on BPM in IT Industry Dessertation project  on BPM in IT Industry
Dessertation project on BPM in IT Industry
Dhrubaji Mandal ♛
 
Business Process Management in IT company
Business Process Management  in IT company Business Process Management  in IT company
Business Process Management in IT company
Dhrubaji Mandal ♛
 
SERVICES MANAGEMENT IN HOTEL INDUSTRY
SERVICES  MANAGEMENT IN HOTEL INDUSTRY SERVICES  MANAGEMENT IN HOTEL INDUSTRY
SERVICES MANAGEMENT IN HOTEL INDUSTRY
Dhrubaji Mandal ♛
 
Supply chain presentation (Mumbai Dabba wala)
Supply chain presentation (Mumbai Dabba wala)Supply chain presentation (Mumbai Dabba wala)
Supply chain presentation (Mumbai Dabba wala)
Dhrubaji Mandal ♛
 
Information technology implementation in power distribution
Information technology implementation in  power distributionInformation technology implementation in  power distribution
Information technology implementation in power distribution
Dhrubaji Mandal ♛
 
Project report on exploring express cargo
Project report on exploring express cargoProject report on exploring express cargo
Project report on exploring express cargo
Dhrubaji Mandal ♛
 
Presentation on job specification
Presentation  on  job specificationPresentation  on  job specification
Presentation on job specification
Dhrubaji Mandal ♛
 
Project Report On Emotion At Work Place -- Dhrubaji Mandal
Project Report On Emotion At Work Place  -- Dhrubaji Mandal Project Report On Emotion At Work Place  -- Dhrubaji Mandal
Project Report On Emotion At Work Place -- Dhrubaji Mandal
Dhrubaji Mandal ♛
 
Ad

Recently uploaded (20)

Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
211421893-M-Tech-CIVIL-Structural-Engineering-pdf.pdf
inmishra17121973
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 

Mongo db dhruba

  • 2. Introduction To MongoDB MongoDB is an open-source, cross-platform, document-oriented database written in C++ that provides high performance, high availability, and automatic scaling. It is developed and supported by a company named 10gen MongoDB is available under General Public license for free, and it is also available under Commercial license from the manufacturer. 2Dhruba Mandal
  • 3. Document oriented database MongoDB stores data as documents, so it is known as document oriented database FirstName = "Dhruba", Address = "New Delhi", Spouse = [{Name: "abcde"}]. FirstName ="mandal", Address = "saket" There are two different documents (separated by ".") Storing data in this manner is called as document oriented database. Example : - 3Dhruba Mandal
  • 4. Purpose of building MongoDB All the modern applications require big data, fast features development, flexible deployment, and the older database systems not competent enough, so the MongoDB was needed. The primary purpose of building MongoDB is: • Scalability : Mongo Db has Scaling capacity from single server deployments to large, complex multi-site architectures • Performance • High Availability 4Dhruba Mandal
  • 5. Features of MongoDB 5Dhruba Mandal 1. Support ad hoc queries : An ad hoc query means query created to obtain information as the need arises. In MongoDB, you can search by field, range query and it also supports regular expression searches. 2. Indexing : We can index any field in a document. Indexes are created to improve the performance of searches. The good thing is that any field in a MongoDB document can be indexed with primary and secondary indices. It enables the database engine to efficiently resolve queries which make it one of the best key features of MongoDB. The database engine can use a predefined index, which maps documents fields and can tell the engine which documents are compatible with this query statement, hence improves performance. 3. Replication : MongoDB supports Master Slave replication , A master can perform Reads and Writes and a Slave copies data from the master and can only be used for reads or back up (not writes) 4. Duplication of data : MongoDB can run over multiple servers. The data is duplicated to keep the system up and also keep its running condition in case of hardware failure.
  • 6. Dhruba Mandal 6 5. Load balancing : It has an automatic load balancing configuration because of data placed in shards. Note : (Sharding is a type of database partitioning that separates very large databases the into smaller, faster, more easily managed parts called data shards) 6 . Supports map reduce and aggregation tools. Map reduce : - Map-reduce is a data processing paradigm (model) for condensing large volumes of data into useful aggregated results. For map-reduce operations, MongoDB provides the mapReduce database command. which collects and condenses(i.e make more concentrated) the aggregated data. Map() procedure performs filtering and sorting Reduce() procedure performs a summary operation db.collection.mapReduce() Aggregation tools : Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result.
  • 7. Dhruba Mandal 7 9. Provides high performance : It uses BSON instead of JSON for storing data. BSON is binary JSON. Being Binary it improves processing efficiency of the MongoDB 10. Stores files of any size easily without complicating your stack. 11 . Easy to administer in the case of failures. 12 . It also supports: ✓ JSON data model with dynamic schemas ✓ Auto-sharding for horizontal scalability ✓ Built in replication for high availability 8. It is a schema-less database written in C++ : MongoDB is a document database in which one collection holds different documents. Number of fields, content and size of the document can differ from one document to another. 7. Uses JavaScript instead of Procedures
  • 8. MongoDB Known as NoSQL Dhruba Mandal 8 We know that MongoDB is a NoSQL Database . Generally Data Base are of 3 types 1. RDBMS (Relational Database Management System) 2. OLAP (Online Analytical Processing) 3. NoSQL (NON SQL ) NoSQL Database is used to refer a non-SQL or non relational database as It provides a mechanism for storage and retrieval of data other than tabular relations model used in relational databases . It is generally used to store big data and real-time web applications.
  • 9. concept of collection and document Dhruba Mandal 9 Database : • Database is a physical container for collections. • Each database gets its own set of files on the file system. • A single MongoDB server typically has multiple databases. Collection • Collection is a group of MongoDB documents. • It is the equivalent of an RDBMS table . • A collection exists within a single database • Collections do not enforce a schema. • Documents within a collection can have different fields Document : • A document is a set of key-value pairs. • Documents have dynamic schema. • Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data
  • 10. Differences in Terminology Dhruba Mandal 10 RDBMS MongoDB Database Database Table Collection Tuple/Row Document column Field Table Join Embedded Documents Primary Key Primary Key (Default key _id provided by mongodb itself)
  • 11. Mongo DB Vs MySQL Dhruba Mandal 11 MongoDB MySQL MongoDB is an open-source database developed by MongoDB, Inc MySQL is a popular open-source relational database management system (RDBMS) that is developed by oracle Corporation No Schema definition required Structured data with clear schema In MongoDB, data represents in a collection of JSON documents In MySQL, data is in tables and rows In MongoDB, each individual records are stored as ‘documents’. In MySQL, each individual records are stored as ‘rows’ in a table. MongoDB’s querying is object-oriented, which means you pass MongoDB a document explaining what you are querying The query language is called Structured Query Language i.e. you have to put a string in the query language that the DB system parses Mongo DB doesn’t support joints, it supports multi-dimensional data types like other documents and arrays. MySQL support join operation In MongoDB, if an index is not found, every document within a collection must be scanned to select the documents that provide a match to the query statement With MySQL, if an index is not defined, the database engine must scan the entire table to find all relevant rows.
  • 12. MongoDB Advantages Dhruba Mandal 12 ❖ MongoDB is schema less. It is a document database in which one collection holds different documents. ❖ There may be difference between number of fields, content and size of the document from one to other. ❖ Structure of a single object is clear in MongoDB. ❖ There are no complex joins in MongoDB. ❖ MongoDB provides the facility of deep query because it supports a powerful dynamic query on documents. ❖ It is very easy to scale. ❖ It uses internal memory for storing working sets and this is the reason of its fast access.
  • 13. MongoDB Data types Dhruba Mandal 13 Data Types Description Arrays This data type is used to store a list or multiple values into a single key. Boolean This data type is used to store Boolean values. It just shows YES/NO values. Date This data type stores the current date or time in Unix time format. It makes you Possible to specify your own date time by creating object of date and pass the value of date, month, year into it. Double Double data type stores floating point values. Integer Integer is used to store the numeric value. It can be 32 bit or 64 bit depending on the server you are using. Min/Max Keys This data type compare a value against the lowest and highest bson elements. Null It is used to store null values. Object Object data type is used for embedded documents. String String is the most commonly used data type. A string must be UTF 8 valid in mongodb. Symbol It is generally used for languages that use a specific type.
  • 14. Installing MongoDB Dhruba Mandal 14 Step 1 – Adding the MongoDB Repository sudo vi /etc/yum.repos.d/mongodb-org.repo 2. Inside mongodb-org.repo write the below configuration [mongodb-org-3.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongod b-org/3.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc 3. Save and Close File by !wq 4. Run command yum repolist The above repolist command displays a list of enabled repositories:
  • 15. Dhruba Mandal 15 5. Now installing MongoDB run below command sudo yum install mongodb-org There are two Is this ok [y/N]: prompts press Y 6. Next, start the MongoDB service with the systemctl utility: sudo systemctl reload mongod sudo systemctl start mongod 7 . check that the service started by viewing the end of the mongod.log file with the tail command: sudo tail /var/log/mongodb/mongod.log . . . [initandlisten] waiting for connections on port 27017 An output of waiting for a connection confirms that MongoDB has started successfully and we can access the database server with the MongoDB Shell: Output
  • 16. Dhruba Mandal 16 8. To login into mongo db Type command mongo Note: When you launched the MongoDB Shell you may have seen a warning like this: ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files. 9. To resolve the warning, alter the processes soft rlimit value for mongod by editing the 20-nproc.conf file: sudo vi /etc/security/limits.d/20-nproc.conf 10. Add the following line to the end of file : mongod soft nproc 32000 PTO
  • 17. Dhruba Mandal 17 11 . For the new limit to be available to MongoDB, restart it using the systemctl utility: sudo systemctl restart mongod 12 . Leave the mongod process running in the background, but quit the shell with the exit command: > exit 13 . Use the systemctl utility to check its startup status systemctl is-enabled mongod; echo $? Output . . . enabled 0 An output of zero confirms an enabled daemon, which we want 14 . In the event of a disabled daemon, use the systemctl utility to enable it : sudo systemctl enable mongod
  • 18. Secure MongoDB with username and password Dhruba Mandal 18 1. Connect to the instance. mongo 2. At the mongo command line, set the administrator: use admin db.createUser( { user: "admin", pwd: "admin123", roles: [ { role: "root", db: "admin" }] } )exit; 3. Authenticate user with below command db.auth("admin", "password") Exit the mongo shell, re-connect, authenticate as the user. 4. Access the MongoDB with below command mongo -u admin -p myadminpassword 127.0.0.1/admin
  • 19. Accessing MongoDB Remotely Dhruba Mandal 19 Once MongoDB credentials is created and Authenticated it is now time to enable MongoDB to start using these access controls To do so . 1. open the file mongod.conf by below command sudo vim /etc/mongod.conf 2. In the file at last add the below line security: authorization: enabled 3. Also in same file change bind IP from 127.0.0.1 to 0.0.0.0 for all or you can give the IP of servers you want . 0.0.0.0. means for all # network interfaces net: port: 27017 bindIp: 0.0.0.0 #default value is 127.0.0.1 4. Restart the MongoDB using below command sudo service mongod restart
  • 20. Dhruba Mandal 20 5. On client machine or machine from which we want to access MongoDB Install the MongoDB- Client . Steps : 1. Create the repository by command sudo vim/etc/yum.repos.d/mongodb.repo 2. Inside mongodb-org.repo write the below configuration [mongodb-org-3.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodborg/3.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc 3. Update the repository index by below command sudo yum update -y
  • 21. Dhruba Mandal 21 4. Install the MongoDB Shell Client with below command sudo yum install mongodb-org-shell -y 5. Now you can connect your mongo DB from client or remote machine by below command mongo -u <username> -p <password> <ip-address>:27017/<collection-name> Example : In my case I am connecting my MongoDB on my Centos 7 machine 10.0.205.53 from my centos7 machine 10.0.205.50 : Output is as below :
  • 22. Concepts of Data Modeling Dhruba Mandal 22 • Data modeling is a representation of the data structures in a table for a company’s database • It is a very powerful expression of the company's business requirements. • Data models are used for many purposes, from high-level conceptual models to physical data models. Three basic styles of data modeling are generally used in practice today. a. Conceptual Data Models(CDMs): High-level, static business structures and concepts b. Logical Data Models (LDMs): Entity types, data attributes and relationships between entities c. Physical Data Models (PDMs): The internal schema database design
  • 23. Data Modeling in MongoDB Dhruba Mandal 23 ➢ Data in MongoDB has a flexible schema. documents in the same collection ➢ The main challenge in data modeling is balancing the need of the application, the performance characteristics of the database engine, and the data retrieval patterns Below are some considerations while designing Schema in MongoDB ✓Always design schema according to user requirements. ✓Combine objects into one document if you will use them together. Otherwise separate them (but make sure there should not be need of joins) ✓Do join on write operations not on read operations. ✓Optimize your schema for most frequent use cases. ✓Do complex aggregation in the schema. ✓ Duplicate the data (but limited) because disk space is cheap as compare to compute time
  • 24. Database and It’s Command in MongoDB Dhruba Mandal 24 Database is a physical container for collections , each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases 1. To create database use command : use Database_name Note : (use database name will create the database if database already exist it will switch to that database) 2. Switching to other database use command : use Database_name 3. To check your currently selected database, use the command : db Note: (It will display your present DB) 4. to check your databases list, use the command : show dbs 5. To delete the database in MongoDB If you want to delete the database 1.First go to that database by command : use Database_name then 2.use command db.dropDatabase()
  • 25. Collection & It's Command in MongoDB Dhruba Mandal 25 Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table . A collection exists within a single database it does not enforce schema so the document with in collection can have different fields. 1. command to create collection . Go to DB use admin and run below command db.createCollection(“name”, “options”) Parameter Type Description Name String Name of the collection to be created Options Document (Optional) Specify options about memory size and indexing Creating collection by name mycollection
  • 26. Dhruba Mandal 26 2. After creating Collection to see the collection use command show collections command show collection will show all the collection as below If we are getting any error like "errmsg" : "not authorized on DB_Name to execute command { create: "mycollection" }", Means the user is not authorized and in this case we need to authorize user by below command use admin; db.grantRolesToUser('admin', [{ role: 'root', db: 'admin' }]) Here in example user is admin but for any user we can do the same Important Note
  • 27. Dhruba Mandal 27 Optional parameter of collections Field Type Description capped Boolean (Optional) If true, enables a capped collection. Capped collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size. ( If you specify true, you need to specify size parameter also.) autoIndexId Boolean (Optional) If true, automatically create index on _id fields Default value is false. size number (Optional) Specifies a maximum size in bytes for a capped collection. If capped is true, then you need to specify this field also. max number (Optional) Specifies the maximum number of documents allowed in the capped collection. Example below shows the syntax of createCollection() method with few important options db.createCollection("mycol", { capped : true, autoIndexId : true, size : 6142800, max : 10000 } ) { "ok" : 1 } >
  • 28. Dhruba Mandal 28 2. Command to drop Collection In MongoDB, db.collection.drop() method is used to drop a collection from a database. It completely removes a collection from the database and does not leave any indexes associated with the dropped collections. Basic syntax : db.COLLECTION_NAME.drop() First check the already existing collections in your database. By command use DB_name (name of the db from where u want to drop collection) show collections db.mycollection.drop() (mycollection = Name of collection you want to drop) If successful it will give output as : true
  • 29. Documents and It’s Command in MongoDB Dhruba Mandal 29 A document is a set of key-value pairs. It has have dynamic schema , it means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data. MongoDB insert documents : There are also two methods "db.collection.update()" method and "db.collection.save()" method used for adding or inserting new documents into a collection in your database Syntax : The basic syntax of insert() command is as follows − db.COLLECTION_NAME.insert(document) > show collections users > db.users.insert({ ... ... id: '1' , ... loginId: 'mandal', ... userPass: 'mandal', ... userType: 'admin', ... active: '1', ... ... })
  • 30. Inserting Multiple record Dhruba Mandal 30 db.users.insert([ {id: '4' , loginId: 'rahul', userPass: 'rahul', userType: 'admin', active: '1', }, { id: '5' , loginId: 'mohit', userPass: 'mohit', userType: 'admin', active: '1', } ]) Here users is collection name Also insert ([ at beginning and write documents inside { } for other use ,
  • 31. MongoDB - Query Document Dhruba Mandal 31 To query data from MongoDB collection, you need to use MongoDB's find() method. Syntax The basic syntax of find() method is as follows − >db.COLLECTION_NAME.find() db.users.find() (In my case users is collection name) To display the results in a formatted way, you can use pretty() method. (same as using G in MySQL ) >db.COLLECTION_NAME.find().pretty() >db.users.find({"loginId":"mandal"}) (like where condition in MySQL )
  • 32. RDBMS Where Clause Equivalents in MongoDB Dhruba Mandal 32 To query the document on the basis of some condition, you can use following operations Operation Syntax Example RDBMS Equivalent Equality {<key>:<value>} db.mycol.find({"by":"mandal"}).pretty() where by = 'mandal' Less Than {<key>:{$lt:<value>}} db.mycol.find({"likes":{$lt:50}}).pretty() where likes < 50 Less Than Equals {<key>:{$lte:<value>}} db.mycol.find({"likes":{$lte:50}}).pretty() where likes <= 50 Greater Than {<key>:{$gt:<value>}} db.mycol.find({"likes":{$gt:50}}).pretty() where likes > 50 Greater Than Equals {<key>:{$gte:<value>}} db.mycol.find({"likes":{$gte:50}}).pretty() where likes >= 50 Not Equals {<key>:{$ne:<value>}} db.mycol.find({"likes":{$ne:50}}).pretty() where likes != 50
  • 33. AND in MongoDB Dhruba Mandal 33 In the find() method, if you pass multiple keys by separating them by ',' then MongoDB treats it as AND condition. Following is the basic syntax of AND − >db.collectionname.find( { $and: [ {"id“: “1”}, {key2:value2} ] } ).pretty()
  • 34. OR in MongoDB Dhruba Mandal 34 To query documents based on the OR condition, you need to use $or keyword. Following is the basic syntax of OR − >db.collectionname.find( { $or: [ {key1: value1}, {key2:value2} ] } ).pretty() We can also use AND and OR Together db.mycol.find({"likes": {$gt:10}, $or: [{"key1: value1"}, {"title": "key2: value2"}]}).pretty()
  • 35. MongoDB - Projection Dhruba Mandal 35 In MongoDB, projection means selecting only the necessary data rather than selecting whole of the data of a document. If a document has 5 fields and you need to show only 3, then select only 3 fields from them. The basic syntax of find() method with projection is as follows − >db.COLLECTION_NAME.find({},{KEY:1}) Example : > db.users.find({},{"loginId":1,_id:0}) { "loginId" : "Sumit" } { "loginId" : "harsh" } { "loginId" : "one97" } { "loginId" : "mohit" } Please note _id field is always displayed while executing find() method, if you don't want this field, then you need to set it as 0.
  • 36. MongoDB - Update Document Dhruba Mandal 36 MongoDB's update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method. MongoDB Update() Method Syntax >db.COLLECTION_NAME.update(SELECTION_CRITERIA,UPDATED_DATA) example > db.users.update({"loginId" : "rahul"},{$set:{"loginId" : "one97"}}) Result : WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) db.users.find() will display the changes
  • 37. Dhruba Mandal 37 MongoDB Save() Method The save() method replaces the existing document with the new document passed in the save() method. Syntax >db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA}) db.users.save( { "_id" : ObjectId("5d6ca5b13212ff16c9e6ef1a"), "userPass":"mandal", "by":"mandal@123" } ) Update is better to use than save
  • 38. MongoDB - Delete Document Dhruba Mandal 38 MongoDB's remove() method is used to remove a document from the collection. remove() method accepts two parameters. One is deletion criteria and second is just One flag. deletion criteria − (Optional) deletion criteria according to documents will be removed. justOne − (Optional) if set to true or 1, then remove only one document. Basic syntax of remove() >db.COLLECTION_NAME.remove(DELLETION_CRITTERIA) db.users.remove({ "userPass" : "mandal"}) Example
  • 39. Dhruba Mandal 39 If there are multiple records and you want to delete only the first record, then set justOne parameter in remove() method. db.COLLECTION_NAME.remove(DELETION_CRITERIA,1) (Note : Here 1 means only first which meets deletion criteria , if multiple rows meet the same criteria only first row will be deleted ) Remove All Documents of that collection >db.collectionmane.remove({}) (Note : This is equivalent of SQL's truncate command )
  • 40. The Limit() and skip() Method Dhruba Mandal 40 To limit the records in MongoDB, you need to use limit() method. The basic syntax of limit() method is as follows − >db.COLLECTION_NAME.find().limit(NUMBER) > db.users.find().limit(2) will only display 2 rows Apart from limit() method, there is one more method skip() which also accepts number type argument and is used to skip the number of documents. The basic syntax of skip() method is as follows db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER) > db.users.find().limit(2).skip(1) i.e. it will skip the number of rows and will show 2 value after that row
  • 41. MongoDB - Sort Records Dhruba Mandal 41 In MongoDB, sort() method is used to sort the documents in the collection. This method accepts a document containing list of fields along with their sorting order. The sorting order is specified as 1 or -1. 1 is used for ascending order sorting. -1 is used for descending order sorting. Basic syntax for sort in as below >db.COLLECTION_NAME.find().sort({KEY:1}) Example : >db.users.find({},{"loginId":1,_id:0}).sort({"loginId" :-1}) >db.users.find({},{"loginId":1,_id:0}).sort({"loginId" : 1})
  • 42. MongoDB - Indexing Dhruba Mandal 42 Indexes are special data structures, that store a small portion of the data set in an easy- to-traverse form. The index stores the value of a specific field or set of fields, ordered by the value of the field as specified in the index To create an index you need to use ensureIndex() method of MongoDB. Syntax >db.COLLECTION_NAME.ensureIndex({KEY:1}) Here key is the name of the field on which you want to create index and 1 is for ascending order-1 create index in descending order db.users.ensureIndex({"loginId":1}) By using ensureIndex() method you can pass multiple fields, to create index on multiple fields. Syntax db.mycol.ensureIndex({"title":1,"description":-1}) db.users.ensureIndex({"loginId":1,"id" :1 })
  • 43. MongoDB - Aggregation Dhruba Mandal 43 Aggregations operations process data records and return computed results In SQL count(*) and with group by is an equivalent of MongoDB aggregation. For the aggregation in MongoDB, you should use aggregate() method. Basic syntax of aggregate() method is as follows − db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION) Example : db.users.aggregate([{$group : {_id : "loginId", num_tutorial : {$sum : 1}}}]) Output : { "_id" : "by_loginId", "num_tutorial" : 4 }
  • 44. Dhruba Mandal 44 list of available aggregation expressions. Expression Description Example $sum Sums up the defined value from all documents in the collection. db.users.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}]) $avg Calculates the average of all given values from all documents in the collection. db.users.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}]) $min Gets the minimum of the corresponding values from all documents in the collection. db.users.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}]) $max Gets the maximum of the corresponding values from all documents in the collection. db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}]) $push Inserts the value to an array in the resulting document. db.users.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}]) $addToSet Inserts the value to an array in the resulting document but does not create duplicates. db.users.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}]) $first Gets the first document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”- stage. db.users.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}]) $last Gets the last document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”- stage. db.users.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}])
  • 45. MongoDB - Replication Dhruba Mandal 45 Replication is the process of synchronizing data across multiple servers. It provides redundancy and increases data availability with multiple copies of data on different database servers 1. Issue the below command to generate your key file: openssl rand -base64 756 > mongo-keyfile 2. Create the /opt/mongo directory to store your key file: sudo mkdir /opt/mongo 3. Move your key file to /opt/mongo and assign it the correct permissions: sudo mv ~/mongo-keyfile /opt/mongo sudo chmod 400 /opt/mongo/mongo-keyfile 4. Update the ownership of your key sudo chown mongod:mongod /opt/mongo/mongo-keyfile 5. Create an Administrative User on server which u choose as primary member of your replication set
  • 46. Dhruba Mandal 46 Create an Administrative User by below steps 5a. log in to the mongo shell by command Mongo 5b. Connect to the admin database: use admin 5c. Create an administrative user with root privileges. Replace “password” with a strong password of your choice: db.createUser({user: "mongo-admin", pwd: "password", roles:[{role: "root", db: "admin"}]}) 6 . Open the configuration file of MongoDB on both server by command sudo vim /etc/mongod.conf 7 In configuration file remove hash (#) from replication and add below lines replSetName: "myreplica01“ ( Important Note : Here myreplica01 is replication name, you can use any name but it should be same on both server also there should be 2 space after replication: i.e. don’t write replSetName just below replication: use 2 space else it will show error )
  • 47. Dhruba Mandal 47 8 . Once changes has been made restart the mongod service by command: sudo systemctl restart mongod 9 . Connect via SSH to the node that you intend to use as your primary mongo -u mongo-admin -p --authenticationDatabase admin Example in my case : mongo -u admin -p admin123 127.0.0.1/admin 10 . From the mongo shell, initiate the replica set: rs.initiate() It will give output as some thing below 11. While still connected to the mongo shell, add the other hosts to the replica set:(any number of set i.e. servers) rs.add("mongod1.net:27017") rs.add("mongod2.net:27017") 12 . Once all members have been added, check the configuration of your replica set: rs.conf() Do any changes on primary and it will replicate on secondary
  • 48. MongoDB - Sharding Dhruba Mandal 48 Sharding is a concept in MongoDB, which splits large data sets into small data sets across multiple MongoDB instances. Sometimes the data within MongoDB will be so huge, that queries against such big data sets can cause a lot of CPU utilization on the server. To tackle this situation, MongoDB has a concept of Sharding, which is basically the splitting of data sets across multiple MongoDB instances. Mongo DB Sharding uses horizontal scaling to solve the problems Concept of Horizontal and Vertical Scaling Horizontal scaling means that you scale by adding more machines into your pool of resources whereas Vertical scaling means that you scale by adding more power (CPU, RAM) to an existing machine.
  • 49. How to Implement Sharding Dhruba Mandal 49 Shards are implemented by using clusters which are nothing but a group of MongoDB instances. 1. A Shard – This is a MongoDB instance which holds the subset of the data. In production environments, all shards need to be part of replica sets 2. Config server – This is a MongoDB instance which holds metadata (i.e. a set of data that describes and gives information about other data) about the cluster, basically information about the various MongoDB instances which will hold the shard data. 3. A Router – This is a MongoDB instance which basically is responsible to re-directing the commands send by the client to the right servers.
  • 50. MongoDB Backup & Restore Dhruba Mandal 50 All backup and restore command will run on node machine not on MongoDB Backup Single Database mongodump --db mydb --out /backup/db/ Ex: - mongodump --db VodaIdeapanel --out /backup/db/ (Here VodaIdeapanel is my db name and /backup/db/ is my path ) Backup All Databases mongodump --out /backup/db/ Backup Single Collection mongodump --collection mycollection --db mydb --out /backup/db/ Example : mongodump --collection user --db VodaIdeapanel --out /backup/db/ (Note here user is my collection name ) Remote database backup specify host, port, username and password for remote databases connections backups like below. mongodump --host 10.0.1.7 --port 27017 --username admin --password some password -- db mydb --out /backup/db/
  • 51. Dhruba Mandal 51 mongorestore is command line tool for restoring MongoDB database backup
  • 52. Restore MongoDB Database Dhruba Mandal 52 mongorestore is command line tool for restoring MongoDB database backup mongorestore --db mydb --drop /backup/db/mydb Example : mongorestore --db pannel --drop /backup/db/VodaIdeapanel (Here /data/db/ is location of your MongoDB data directory and /backup/db is location of backup directory.) MongoDB Backup Shell Script #!/bin/sh TODAY=`date +%d%b%Y` BACKUP_DIR=/backup/db mkdir -p ${BACKUP_DIR}/${TODAY} mongodump -h <DATABASE_HOST> -d <DATABASE_NAME> -u <USERNAME> -p <PASSWRD> --out ${BACKUP_DIR}/${TODAY}/
  • 53. When to use MangoDB /MySQL Dhruba Mandal 53 MongoDB MySQL High Availability : When you need high Availability of data with automatic fast and instant data recovery Low Maintenance: When your just starting and your database is not going to scale much, MySQL will help you in easy and low maintenance set up IN Built Sharding : In future, if you're going to grow big as MangoDB has inbuilt Sharding solution Limited Budget: if you want high performance in limited budget Unstable Schema: if you have an unstable schema and you want to reduce your schema migration cost Fixed Schema: If you have fixed schema and data structure isn't going to change over the time like Wikipedia NO DBA : if you don't have Database Administrator (required for larger) High Transaction: If high transaction rate is going to be your requirement (i.e. high insert or select) Cloud Computing: If most of your services are cloud-based , MongoDB is best suitable Data Security: If data security is your top priority , MySQL is the most secure DBMS
  • 54. References Dhruba Mandal 54 https://www.tutorialsjar.com/key-features-of-mongodb/ https://www.quora.com/How-can-Mongo-db-provides-high-performance https://www.mongodb.com/nosql-explained https://www.digitalocean.com/community/tutorials/how-to-install-mongodb-on-centos-7 https://www.javatpoint.com/mongodb-tutorial https://stackoverflow.com/questions/4881208/how-to-secure-mongodb-with-username- and-password https://stackoverflow.com/questions/23943651/mongodb-admin-user-not-authorized https://www.linode.com/docs/databases/mongodb/create-a-mongodb-replica-set/ http://www.dba86.com/docs/mongo/2.4/reference/limits.html https://www.howtoforge.com/tutorial/deploying-mongodb-sharded-cluster-on-centos- 7/#three-sharding-components https://tecadmin.net/backup-and-restore-mongodb-database/