MongoDB Quick Guide
MongoDB Quick Guide
MONGODB - OVERVIEW
MongoDB is a cross-platform, document oriented database that provides, high performance, high availability,
and easy scalability. MongoDB works on concept of collection and document.
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. Typically, all documents in a collection are of similar or related purpose.
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.
The following table shows the relationship of RDBMS terminology with MongoDB.
RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Mysqld/Oracle mongod
mysql/sqlplus mongo
Sample Document
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 1/61
03/08/2017 MongoDB Quick Guide
Following example shows the document structure of a blog site, which is simply a comma separated key value
pair.
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2011,1,20,2,15),
like: 0
},
{
user:'user2',
message: 'My second comments',
dateCreated: new Date(2011,1,25,7,45),
like: 5
}
]
}
_id is a 12 bytes hexadecimal number which assures the uniqueness of every document. You can provide _id
while inserting the document. If you dont provide then MongoDB provides a unique id for every document.
These 12 bytes first 4 bytes for the current timestamp, next 3 bytes for machine id, next 2 bytes for process id
of MongoDB server and remaining 3 bytes are simple incremental VALUE.
MONGODB - ADVANTAGES
Any relational database has a typical schema design that shows number of tables and the relationship between
these tables. While in MongoDB, there is no concept of relationship.
No complex joins.
Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query
language that's nearly as powerful as SQL.
Tuning.
Uses internal memory for storing the windowed working set, enabling faster access of data.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 2/61
03/08/2017 MongoDB Quick Guide
Document Oriented Storage Data is stored in the form of JSON style documents.
Auto-sharding
Rich queries
MONGODB - ENVIRONMENT
Let us now see how to install MongoDB on Windows.
32-bit versions of MongoDB only support databases smaller than 2GB and suitable only for testing and
evaluation purposes.
Now extract your downloaded file to c:\ drive or any other location. Make sure the name of the extracted
folder is mongodb-win32-i386-[version] or mongodb-win32-x86_64-[version]. Here [version] is the version
of MongoDB download.
Next, open the command prompt and run the following command.
In case you have extracted the MongoDB at different location, then go to that path by using command cd
FOLDER/DIR and now run the above given process.
MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is
c:\data\db. So you need to create this folder using the Command Prompt. Execute the following command
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 3/61
03/08/2017 MongoDB Quick Guide
sequence.
C:\>md data
C:\md data\db
If you have to install the MongoDB at a different location, then you need to specify an alternate path for
\data\db by setting the path dbpath in mongod.exe. For the same, issue the following commands.
In the command prompt, navigate to the bin directory present in the MongoDB installation folder. Suppose
my installation folder is D:\set up\mongodb
C:\Users\XYZ>d:
D:\>cd "set up"
D:\set up>cd mongodb
D:\set up\mongodb>cd bin
D:\set up\mongodb\bin>mongod.exe --dbpath "d:\set up\mongodb\data"
This will show waiting for connections message on the console output, which indicates that the
mongod.exe process is running successfully.
Now to run the MongoDB, you need to open another command prompt and issue the following command.
D:\set up\mongodb\bin>mongo.exe
MongoDB shell version: 2.4.6
connecting to: test
>db.test.save( { a: 1 } )
>db.test.find()
{ "_id" : ObjectId(5879b0f65a56a454), "a" : 1 }
>
This will show that MongoDB is installed and run successfully. Next time when you run MongoDB, you need
to issue only commands.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 4/61
03/08/2017 MongoDB Quick Guide
In the above installation, 2.2.3 is currently released MongoDB version. Make sure to install the latest version
always. Now MongoDB is installed successfully.
Start MongoDB
Stop MongoDB
Restart MongoDB
mongo
MongoDB Help
To get a list of commands, type db.help in MongoDB client. This will give you a list of commands as shown in
the following screenshot.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 5/61
03/08/2017 MongoDB Quick Guide
MongoDB Statistics
To get stats about MongoDB server, type the command db.stats in MongoDB client. This will show the
database name, number of collection and documents in the database. Output of the command is shown in the
following screenshot.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 6/61
03/08/2017 MongoDB Quick Guide
Data in MongoDB has a flexible schema.documents in the same collection. They do not need to have the same
set of fields or structure, and common fields in a collections documents may hold different types of data.
Combine objects into one document if you will use them together. Otherwise separate them
butmakesurethereshouldnotbeneedof joins.
Duplicate the data butlimited because disk space is cheap as compare to compute time.
Example
Suppose a client needs a database design for his blog/website and see the differences between RDBMS and
MongoDB schema design. Website has the following requirements.
In RDBMS schema, design for above requirements will have minimum three tables.
While in MongoDB schema, design will have one collection post and the following structure
{
_id: POST_ID
title: TITLE_OF_POST,
description: POST_DESCRIPTION,
by: POST_BY,
url: URL_OF_POST,
tags: [TAG1, TAG2, TAG3],
likes: TOTAL_LIKES,
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 7/61
03/08/2017 MongoDB Quick Guide
comments: [
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
},
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
}
]
}
So while showing the data, in RDBMS you need to join three tables and in MongoDB, data will be shown from
one collection only.
Syntax
use DATABASE_NAME
Example
If you want to create a database with name <mydb>, then use DATABASE statement would be as follows
>use mydb
switched to db mydb
>db
mydb
If you want to check your databases list, use the command show dbs.
>show dbs
local 0.78125GB
test 0.23012GB
Your created database mydb is not present in list. To display database, you need to insert at least one
document into it.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 8/61
03/08/2017 MongoDB Quick Guide
>db.movie.insert({"name":"tutorials point"})
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
In MongoDB default database is test. If you didn't create any database, then collections will be stored in test
database.
Syntax
db.dropDatabase()
This will delete the selected database. If you have not selected any database, then it will delete default 'test'
database.
Example
First, check the list of available databases by using the command, show dbs.
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
>
If you want to delete new database <mydb>, then dropDatabase command would be as follows
>use mydb
switched to db mydb
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }
>
>show dbs
local 0.78125GB
test 0.23012GB
>
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 9/61
03/08/2017 MongoDB Quick Guide
Syntax
db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a document and is used to specify
configuration of collection.
Options parameter is optional, so you need to specify only the name of the collection. Following is the list of
options you can use
While inserting the document, MongoDB first checks size field of capped collection, then it checks max field.
Examples
>use test
switched to db test
>db.createCollection("mycollection")
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 10/61
03/08/2017 MongoDB Quick Guide
{ "ok" : 1 }
>
You can check the created collection by using the command show collections.
>show collections
mycollection
system.indexes
The following example shows the syntax of createCollection method with few important options
In MongoDB, you don't need to create collection. MongoDB creates collection automatically, when you insert
some document.
>db.tutorialspoint.insert({"name" : "tutorialspoint"})
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>
Syntax
db.COLLECTION_NAME.drop()
Example
First, check the available collections into your database mydb.
>use mydb
switched to db mydb
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 11/61
03/08/2017 MongoDB Quick Guide
>db.mycollection.drop()
true
>
>show collections
mycol
system.indexes
tutorialspoint
>
drop method will return true, if the selected collection is dropped successfully, otherwise it will return false.
MONGODB - DATATYPES
MongoDB supports many datatypes. Some of them are
String This is the most commonly used datatype to store the data. String in MongoDB must be UTF-
8 valid.
Integer This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon
your server.
Min/ Max keys This type is used to compare a value against the lowest and highest BSON elements.
Arrays This type is used to store arrays or list or multiple values into one key.
Timestamp ctimestamp. This can be handy for recording when a document has been modified or
added.
Symbol This datatype is used identically to a string; however, it's generally reserved for languages
that use a specific symbol type.
Date This datatype is used to store the current date or time in UNIX time format. You can specify
your own date time by creating object of Date and passing day, month, year into it.
Code This datatype is used to store JavaScript code into the document.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 12/61
03/08/2017 MongoDB Quick Guide
Syntax
>db.COLLECTION_NAME.insert(document)
Example
>db.mycol.insert({
_id: ObjectId(7df78ad8902c),
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
})
Here mycol is our collection name, as created in the previous chapter. If the collection doesn't exist in the
database, then MongoDB will create this collection and then insert a document into it.
In the inserted document, if we don't specify the _id parameter, then MongoDB assigns a unique ObjectId for
this document.
_id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes are divided as follows
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id,
3 bytes incrementer)
To insert multiple documents in a single query, you can pass an array of documents in insert command.
Example
>db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: "NoSQL database doesn't have tables",
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 13/61
03/08/2017 MongoDB Quick Guide
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
])
To insert the document you can use db.post.savedocument also. If you don't specify _id in the document
then save method will work same as insert method. If you specify _id then it will replace whole data of
document containing _id as specified in save method.
Syntax
>db.COLLECTION_NAME.find()
Syntax
>db.mycol.find().pretty()
Example
>db.mycol.find().pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tutorials point",
"url": "http://www.tutorialspoint.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
Apart from find method, there is findOne method, that returns only one document.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 14/61
03/08/2017 MongoDB Quick Guide
<value>}}
Less Than Equals {<key>: db.mycol.find" likes .pretty where likes <= 50
{$lte: ": $lte : 50
<value>}}
<value>}}
Greater Than Equals {<key>: db.mycol.find" likes .pretty where likes >= 50
{$gte: ": $gte : 50
<value>}}
<value>}}
AND in MongoDB
Syntax
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.mycol.find(
{
$and: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
Following example will show all the tutorials written by 'tutorials point' and whose title is 'MongoDB
Overview'.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 15/61
03/08/2017 MongoDB Quick Guide
For the above given example, equivalent where clause will be ' where by = 'tutorials point' AND title =
'MongoDB Overview' '. You can pass any number of key, value pairs in find clause.
OR in MongoDB
Syntax
To query documents based on the OR condition, you need to use $or keyword. Following is the basic syntax of
OR
>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
Following example will show all the tutorials written by 'tutorials point' or whose title is 'MongoDB Overview'.
Example
The following example will show the documents that have likes greater than 10 and whose title is either
'MongoDB Overview' or by is 'tutorials point'. Equivalent SQL where clause is 'where likes>10 AND
by = tutorialspoint ORtitle = M ongoDBOverview '
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 16/61
03/08/2017 MongoDB Quick Guide
}
>
Syntax
>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)
Example
Consider the mycol collection has the following data.
Following example will set the new title 'New MongoDB Tutorial' of the documents whose title is 'MongoDB
Overview'.
By default, MongoDB will update only a single document. To update multiple documents, you need to set a
parameter 'multi' to true.
>db.mycol.update({'title':'MongoDB Overview'},
{$set:{'title':'New MongoDB Tutorial'}},{multi:true})
Syntax
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
Example
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 17/61
03/08/2017 MongoDB Quick Guide
Following example will replace the document with the _id '5983548781331adf45ec7'.
>db.mycol.save(
{
"_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point New Topic",
"by":"Tutorials Point"
}
)
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"Tutorials Point New Topic",
"by":"Tutorials Point"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
>
Syntax
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
Example
Consider the mycol collection has the following data.
Following example will remove all the documents whose title is 'MongoDB Overview'.
>db.mycol.remove({'title':'MongoDB Overview'})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
>
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 18/61
03/08/2017 MongoDB Quick Guide
>db.mycol.remove()
>db.mycol.find()
>
MONGODB - PROJECTION
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.
Syntax
>db.COLLECTION_NAME.find({},{KEY:1})
Example
Following example will display the title of the document while querying the document.
>db.mycol.find({},{"title":1,_id:0})
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}
{"title":"Tutorials Point Overview"}
>
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.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 19/61
03/08/2017 MongoDB Quick Guide
To limit the records in MongoDB, you need to use limit method. The method accepts one number type
argument, which is the number of documents that you want to be displayed.
Syntax
>db.COLLECTION_NAME.find().limit(NUMBER)
Example
Following example will display only two documents while querying the document.
>db.mycol.find({},{"title":1,_id:0}).limit(2)
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}
>
If you don't specify the number argument in limit method then it will display all documents from the
collection.
Syntax
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
Example
Following example will display only the second document.
>db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1)
{"title":"NoSQL Overview"}
>
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 20/61
03/08/2017 MongoDB Quick Guide
To sort documents in MongoDB, you need to use sort method. The method accepts a document containing a
list of fields along with their sorting order. To specify sorting order 1 and -1 are used. 1 is used for ascending
order while -1 is used for descending order.
Syntax
>db.COLLECTION_NAME.find().sort({KEY:1})
Example
Following example will display the documents sorted by title in the descending order.
>db.mycol.find({},{"title":1,_id:0}).sort({"title":-1})
{"title":"Tutorials Point Overview"}
{"title":"NoSQL Overview"}
{"title":"MongoDB Overview"}
>
Please note, if you don't specify the sorting preference, then sort method will display the documents in
ascending order.
MONGODB - INDEXING
Indexes support the efficient resolution of queries. Without indexes, MongoDB must scan every document of a
collection to select those documents that match the query statement. This scan is highly inefficient and require
MongoDB to process a large volume of data.
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.
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. To create
index in descending order you need to use -1.
Example
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 21/61
03/08/2017 MongoDB Quick Guide
>db.mycol.ensureIndex({"title":1})
>
In ensureIndex method you can pass multiple fields, to create index on multiple fields.
>db.mycol.ensureIndex({"title":1,"description":-1})
>
ensureIndex method also accepts list of options whichareoptional. Following is the list
Builds the index in the background so that building an index does not
background Boolean block other database activities. Specify true to build in the background.
The default value is false.
Creates a unique index so that the collection will not accept insertion of
unique Boolean documents where the index key or keys match an existing value in the
index. Specify true to create a unique index. The default value is false.
If true, the index only references documents with the specified field.
sparse Boolean These indexes use less space but behave differently in some situations
particularlysorts . The default value is false.
index The index version number. The default index version depends on the
v
version version of MongoDB running when creating the index.
For a text index, the language that determines the list of stop words
default_language string and the rules for the stemmer and tokenizer. The default value is
english.
language_override string For a text index, specify the name of the field in the document that
contains, the language to override the default language. The default
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 22/61
03/08/2017 MongoDB Quick Guide
value is language.
MONGODB - AGGREGATION
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. In SQL count and with group by is an equivalent of mongodb aggregation.
Syntax
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Example
In the collection you have the following data
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by_user: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
_id: ObjectId(7df78ad8902d)
title: 'NoSQL Overview',
description: 'No sql database is very fast',
by_user: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 10
},
{
_id: ObjectId(7df78ad8902e)
title: 'Neo4j Overview',
description: 'Neo4j is no sql database',
by_user: 'Neo4j',
url: 'http://www.neo4j.com',
tags: ['neo4j', 'database', 'NoSQL'],
likes: 750
},
Now from the above collection, if you want to display a list stating how many tutorials are written by each
user, then you will use the following aggregate method
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 23/61
03/08/2017 MongoDB Quick Guide
"result" : [
{
"_id" : "tutorials point",
"num_tutorial" : 2
},
{
"_id" : "Neo4j",
"num_tutorial" : 1
}
],
"ok" : 1
}
>
Sql equivalent query for the above use case will be select by_user, count from mycol group by
by_user.
In the above example, we have grouped documents by field by_user and on each occurrence of by_user
previous value of sum is incremented. Following is a list of available aggregation expressions.
Inserts the value to an array in the db.mycol.aggregate[$group : id :" $byu ser ", url
$addToSet resulting document but does not : id :" $byu ser ", url
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 24/61
03/08/2017 MongoDB Quick Guide
sense together with some previously : id :" $byu ser ", f irstu rl
$last grouping. Typically this makes only : $last :" $url "
Pipeline Concept
In UNIX command, shell pipeline means the possibility to execute an operation on some input and use the
output as the input for the next command and so on. MongoDB also supports same concept in aggregation
framework. There is a set of possible stages and each of those is taken as a set of documents as an input and
produces a resulting set of documents orthef inalresultingJ S ON documentattheendof thepipeline. This can
then in turn be used for the next stage and so on.
$match This is a filtering operation and thus this can reduce the amount of documents that are given
as input to the next stage.
$skip With this, it is possible to skip forward in the list of documents for a given amount of
documents.
$limit This limits the amount of documents to look at, by the given number starting from the current
positions.
$unwind This is used to unwind document that are using arrays. When using an array, the data is
kind of pre-joined and this operation will be undone with this to have individual documents again. Thus
with this stage we will increase the amount of documents for the next stage.
MONGODB - REPLICATION
Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and
increases data availability with multiple copies of data on different database servers. Replication protects a
database from the loss of a single server. Replication also allows you to recover from hardware failure and
service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting,
or backup.
Why Replication?
To keep your data safe
High 24 7 availability of data
Disaster recovery
No downtime for maintenance likebackups, indexrebuilds, compaction
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 25/61
03/08/2017 MongoDB Quick Guide
In a replica set, one node is primary node and remaining nodes are secondary.
At the time of automatic failover or maintenance, election establishes for primary and a new primary
node is elected.
After the recovery of failed node, it again join the replica set and works as a secondary node.
A typical diagram of MongoDB replication is shown in which client application always interact with the
primary node and the primary node then replicates the data to the secondary nodes.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 26/61
03/08/2017 MongoDB Quick Guide
Automatic failover
Automatic recovery
Consensus election of primary
Start the MongoDB server by specifying -- replSet option. Following is the basic syntax of --replSet
Example
It will start a mongod instance with the name rs0, on port 27017.
Now start the command prompt and connect to this mongod instance.
In Mongo client, issue the command rs.initiate to initiate a new replica set.
To check the replica set configuration, issue the command rs.conf. To check the status of replica set
issue the command rs.status.
Syntax
>rs.add(HOST_NAME:PORT)
Example
Suppose your mongod instance name is mongod1.net and it is running on port 27017. To add this instance
to replica set, issue the command rs.add in Mongo client.
>rs.add("mongod1.net:27017")
>
You can add mongod instance to replica set only when you are connected to primary node. To check whether
you are connected to primary or not, issue the command db.isMaster in mongo client.
MONGODB - SHARDING
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 27/61
03/08/2017 MongoDB Quick Guide
Sharding is the process of storing data records across multiple machines and it is MongoDB's approach to
meeting the demands of data growth. As the size of the data increases, a single machine may not be sufficient
to store the data nor provide an acceptable read and write throughput. Sharding solves the problem with
horizontal scaling. With sharding, you add more machines to support data growth and the demands of read
and write operations.
Why Sharding?
In replication, all writes go to master node
Latency sensitive queries still go to master
Single replica set has limitation of 12 nodes
Memory can't be large enough when active dataset is big
Local disk is not big enough
Vertical scaling is too expensive
Sharding in MongoDB
The following diagram shows the sharding in MongoDB using sharded cluster.
Shards Shards are used to store data. They provide high availability and data consistency. In
production environment, each shard is a separate replica set.
Config Servers Config servers store the cluster's metadata. This data contains a mapping of the
cluster's data set to the shards. The query router uses this metadata to target operations to specific
shards. In production environment, sharded clusters have exactly 3 config servers.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 28/61
03/08/2017 MongoDB Quick Guide
Query Routers Query routers are basically mongo instances, interface with client applications and
direct operations to the appropriate shard. The query router processes and targets the operations to
shards and then returns results to the clients. A sharded cluster can contain more than one query router
to divide the client request load. A client sends requests to one query router. Generally, a sharded
cluster have many query routers.
Syntax
>mongodump
Example
Start your mongod server. Assuming that your mongod server is running on the localhost and port 27017,
open a command prompt and go to the bin directory of your mongodb instance and type the command
mongodump
>mongodump
The command will connect to the server running at 127.0.0.1 and port 27017 and back all data of the server
to directory /bin/dump/. Following is the output of the command
Following is a list of available options that can be used with the mongodump command.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 29/61
03/08/2017 MongoDB Quick Guide
mongodump --host HOST_NAME --port This commmand will backup mongodump --host
PORT_NUMBER all databases of specified tutorialspoint.com --port
mongod instance. 27017
mongodump --dbpath DB_PATH --out This command will backup mongodump --dbpath
BACKUP_DIRECTORY only specified database at /data/db/ --out
specified path. /data/backup/
Restore data
To restore backup data MongoDB's mongorestore command is used. This command restores all of the data
from the backup directory.
Syntax
>mongorestore
MONGODB - DEPLOYMENT
When you are preparing a MongoDB deployment, you should try to understand how your application is going
to hold up in production. Its a good idea to develop a consistent, repeatable approach to managing your
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 30/61
03/08/2017 MongoDB Quick Guide
deployment environment so that you can minimize any surprises once youre in production.
The best approach incorporates prototyping your set up, conducting load testing, monitoring key metrics, and
using that information to scale your set up. The key part of the approach is to proactively monitor your entire
system - this will help you understand how your production system will hold up before deploying, and
determine where you will need to add capacity. Having insight into potential spikes in your memory usage, for
example, could help put out a write-lock fire before it starts.
mongostat
This command checks the status of all running mongod instances and return counters of database operations.
These counters include inserts, queries, updates, deletes, and cursors. Command also shows when youre
hitting page faults, and showcase your lock percentage. This means that you're running low on memory,
hitting write capacity or have some performance issue.
To run the command, start your mongod instance. In another command prompt, go to bin directory of your
mongodb installation and type mongostat.
D:\set up\mongodb\bin>mongostat
mongotop
This command tracks and reports the read and write activity of MongoDB instance on a collection basis. By
default, mongotop returns information in each second, which you can change it accordingly. You should
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 31/61
03/08/2017 MongoDB Quick Guide
check that this read and write activity matches your application intention, and youre not firing too many
writes to the database at a time, reading too frequently from a disk, or are exceeding your working set size.
To run the command, start your mongod instance. In another command prompt, go to bin directory of your
mongodb installation and type mongotop.
D:\set up\mongodb\bin>mongotop
To change mongotop command to return information less frequently, specify a specific number after the
mongotop command.
D:\set up\mongodb\bin>mongotop 30
Apart from the MongoDB tools, 10gen provides a free, hosted monitoring service, MongoDB Management
Service M M S , that provides a dashboard and gives you a view of the metrics from your entire cluster.
MONGODB - JAVA
In this chapter, we will learn how to set up MongoDB JDBC driver.
Installation
Before you start using MongoDB in your Java programs, you need to make sure that you have MongoDB
JDBC driver and Java set up on the machine. You can check Java tutorial for Java installation on your
machine. Now, let us check how to set up MongoDB JDBC driver.
You need to download the jar from the path Download mongo.jar. Make sure to download the latest
release of it.
Connect to Database
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 32/61
03/08/2017 MongoDB Quick Guide
To connect database, you need to specify the database name, if the database doesn't exist then MongoDB
creates it automatically.
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
Now, let's compile and run the above program to create our database myDb as shown below.
$javac ConnectToDB.java
$java ConnectToDB
Create a Collection
To create a collection, createCollection method of com.mongodb.client.MongoDatabase class is used.
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 33/61
03/08/2017 MongoDB Quick Guide
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
//Creating a collection
database.createCollection("sampleCollection");
System.out.println("Collection created successfully");
}
}
Getting/Selecting a Collection
To get/select a collection from the database, getCollection method of
com.mongodb.client.MongoDatabase class is used.
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Creating a collection
System.out.println("Collection created successfully");
// Retieving a collection
MongoCollection<Document> collection = database.getCollection("myCollection");
System.out.println("Collection myCollection selected successfully");
}
}
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 34/61
03/08/2017 MongoDB Quick Guide
Insert a Document
To insert a document into MongoDB, insert method of com.mongodb.client.MongoCollection class is
used.
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Retrieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
System.out.println("Collection sampleCollection selected successfully");
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 35/61
03/08/2017 MongoDB Quick Guide
To select all documents from the collection, find method of com.mongodb.client.MongoCollection class
is used. This method returns a cursor, so you need to iterate this cursor.
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Retrieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
System.out.println("Collection sampleCollection selected successfully");
while (it.hasNext()) {
System.out.println(it.next());
i++;
}
}
}
Document{{
_id = 5967745223993a32646baab8,
title = MongoDB,
id = 1,
description = database,
likes = 100,
url = http://www.tutorialspoint.com/mongodb/, by = tutorials point
}}
Document{{
_id = 7452239959673a32646baab8,
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 36/61
03/08/2017 MongoDB Quick Guide
title = RethinkDB,
id = 2,
description = database,
likes = 200,
url = http://www.tutorialspoint.com/rethinkdb/, by = tutorials point
}}
Update Document
To update a document from the collection, updateOne method of
com.mongodb.client.MongoCollection class is used.
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Retrieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
System.out.println("Collection myCollection selected successfully");
while (it.hasNext()) {
System.out.println(it.next());
i++;
}
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 37/61
03/08/2017 MongoDB Quick Guide
}
}
Delete a Document
To delete a document from the collection, you need to use the deleteOne method of the
com.mongodb.client.MongoCollection class.
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import java.util.Iterator;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Retrieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
System.out.println("Collection sampleCollection selected successfully");
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 38/61
03/08/2017 MongoDB Quick Guide
while (it.hasNext()) {
System.out.println("Inserted Document: "+i);
System.out.println(it.next());
i++;
}
}
}
Dropping a Collection
To drop a collection from a database, you need to use the drop method of the
com.mongodb.client.MongoCollection class.
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
System.out.println("Connected to the database successfully");
// Creating a collection
System.out.println("Collections created successfully");
// Retieving a collection
MongoCollection<Document> collection = database.getCollection("sampleCollection");
// Dropping a Collection
collection.drop();
System.out.println("Collection dropped successfully");
}
}
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 39/61
03/08/2017 MongoDB Quick Guide
import com.mongodb.client.MongoDatabase;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
// Creating Credentials
MongoCredential credential;
credential = MongoCredential.createCredential("sampleUser", "myDb",
"password".toCharArray());
Remaining MongoDB methods save, limit, skip, sort etc. work same as explained in the subsequent
tutorial.
MONGODB - PHP
To use MongoDB with PHP, you need to use MongoDB PHP driver. Download the driver from the url
Download PHP Driver. Make sure to download the latest release of it. Now unzip the archive and put
php_mongo.dll in your PHP extension directory " ext " bydef ault and add the following line to your php.ini
file
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 40/61
03/08/2017 MongoDB Quick Guide
extension = php_mongo.dll
<?php
// connect to mongodb
$m = new MongoClient();
Create a Collection
Following is the code snippet to create a collection
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->createCollection("mycol");
echo "Collection created succsessfully";
?>
Insert a Document
To insert a document into MongoDB, insert method is used.
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 41/61
03/08/2017 MongoDB Quick Guide
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
$document = array(
"title" => "MongoDB",
"description" => "database",
"likes" => 100,
"url" => "http://www.tutorialspoint.com/mongodb/",
"by", "tutorials point"
);
$collection->insert($document);
echo "Document inserted successfully";
?>
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
$cursor = $collection->find();
// iterate cursor to display title of documents
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 42/61
03/08/2017 MongoDB Quick Guide
Update a Document
To update a document, you need to use the update method.
In the following example, we will update the title of inserted document to MongoDB Tutorial. Following is
the code snippet to update a document
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
$collection = $db->mycol;
echo "Collection selected succsessfully";
Delete a Document
To delete a document, you need to use remove method.
In the following example, we will remove the documents that has the title MongoDB Tutorial. Following is
the code snippet to delete a document
<?php
// connect to mongodb
$m = new MongoClient();
echo "Connection to database successfully";
// select a database
$db = $m->mydb;
echo "Database mydb selected";
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 43/61
03/08/2017 MongoDB Quick Guide
$collection = $db->mycol;
echo "Collection selected succsessfully";
In the above example, the second parameter is boolean type and used for justOne field of remove method.
Remaining MongoDB methods findOne, save, limit, skip, sort etc. works same as explained above.
MONGODB - RELATIONSHIPS
Relationships in MongoDB represent how various documents are logically related to each other. Relationships
can be modeled via Embedded and Referenced approaches. Such relationships can be either 1:1, 1:N, N:1
or N:N.
Let us consider the case of storing addresses for users. So, one user can have multiple addresses making this a
1:N relationship.
{
"_id":ObjectId("52ffc33cd85242f436000001"),
"name": "Tom Hanks",
"contact": "987654321",
"dob": "01-01-1991"
}
{
"_id":ObjectId("52ffc4a5d85242602e000000"),
"building": "22 A, Indiana Apt",
"pincode": 123456,
"city": "Los Angeles",
"state": "California"
}
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 44/61
03/08/2017 MongoDB Quick Guide
{
"_id":ObjectId("52ffc33cd85242f436000001"),
"contact": "987654321",
"dob": "01-01-1991",
"name": "Tom Benzamin",
"address": [
{
"building": "22 A, Indiana Apt",
"pincode": 123456,
"city": "Los Angeles",
"state": "California"
},
{
"building": "170 A, Acropolis Apt",
"pincode": 456789,
"city": "Chicago",
"state": "Illinois"
}
]
}
This approach maintains all the related data in a single document, which makes it easy to retrieve and
maintain. The whole document can be retrieved in a single query such as
>db.users.findOne({"name":"Tom Benzamin"},{"address":1})
Note that in the above query, db and users are the database and collection respectively.
The drawback is that if the embedded document keeps on growing too much in size, it can impact the
read/write performance.
{
"_id":ObjectId("52ffc33cd85242f436000001"),
"contact": "987654321",
"dob": "01-01-1991",
"name": "Tom Benzamin",
"address_ids": [
ObjectId("52ffc4a5d85242602e000000"),
ObjectId("52ffc4a5d85242602e000001")
]
}
As shown above, the user document contains the array field address_ids which contains ObjectIds of
corresponding addresses. Using these ObjectIds, we can query the address documents and get address details
from there. With this approach, we will need two queries: first to fetch the address_ids fields from user
document and second to fetch these addresses from address collection.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 45/61
03/08/2017 MongoDB Quick Guide
address, it also needs to specify which collection to look into based on the address type. In such scenarios
where a document references documents from many collections, we should use DBRefs.
Using DBRefs
There are three fields in DBRefs
$id This field specifies the _id field of the referenced document
$db This is an optional field and contains the name of the database in which the referenced
document lies
Consider a sample user document having DBRef field address as shown in the code snippet
{
"_id":ObjectId("53402597d852426020000002"),
"address": {
"$ref": "address_home",
"$id": ObjectId("534009e4d852427820000002"),
"$db": "tutorialspoint"},
"contact": "987654321",
"dob": "01-01-1991",
"name": "Tom Benzamin"
}
The address DBRef field here specifies that the referenced address document lies in address_home
collection under tutorialspoint database and has an id of 534009e4d852427820000002.
The following code dynamically looks in the collection specified by $ref parameter (address_home in our
case) for a document with id as specified by $id parameter in DBRef.
The above code returns the following address document present in address_home collection
{
"_id" : ObjectId("534009e4d852427820000002"),
"building" : "22 A, Indiana Apt",
"pincode" : 123456,
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 46/61
03/08/2017 MongoDB Quick Guide
Since all the fields present in the query are part of an index, MongoDB matches the query conditions and
returns the result using the same index without actually looking inside the documents. Since indexes are
present in RAM, fetching data from indexes is much faster as compared to fetching data by scanning
documents.
{
"_id": ObjectId("53402597d852426020000002"),
"contact": "987654321",
"dob": "01-01-1991",
"gender": "M",
"name": "Tom Benzamin",
"user_name": "tombenzamin"
}
We will first create a compound index for the users collection on the fields gender and user_name using
the following query
>db.users.ensureIndex({gender:1,user_name:1})
>db.users.find({gender:"M"},{user_name:1,_id:0})
That is to say that for the above query, MongoDB would not go looking into database documents. Instead it
would fetch the required data from indexed data which is very fast.
Since our index does not include _id field, we have explicitly excluded it from result set of our query, as
MongoDB by default returns _id field in every query. So the following query would not have been covered
inside the index created above
>db.users.find({gender:"M"},{user_name:1})
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 47/61
03/08/2017 MongoDB Quick Guide
Using $explain
The $explain operator provides information on the query, indexes used in a query and other statistics. It is
very useful when analyzing how well your indexes are optimized.
In the last chapter, we had already created an index for the users collection on fields gender and
user_name using the following query
>db.users.ensureIndex({gender:1,user_name:1})
>db.users.find({gender:"M"},{user_name:1,_id:0}).explain()
{
"cursor" : "BtreeCursor gender_1_user_name_1",
"isMultiKey" : false,
"n" : 1,
"nscannedObjects" : 0,
"nscanned" : 1,
"nscannedObjectsAllPlans" : 0,
"nscannedAllPlans" : 1,
"scanAndOrder" : false,
"indexOnly" : true,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"gender" : [
[
"M",
"M"
]
],
"user_name" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
}
}
The true value of indexOnly indicates that this query has used indexing.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 48/61
03/08/2017 MongoDB Quick Guide
The cursor field specifies the type of cursor used. BTreeCursor type indicates that an index was used
and also gives the name of the index used. BasicCursor indicates that a full scan was made without
using any indexes.
Using $hint
The $hint operator forces the query optimizer to use the specified index to run a query. This is particularly
useful when you want to test performance of a query with different indexes. For example, the following query
specifies the index on fields gender and user_name to be used for this query
>db.users.find({gender:"M"},{user_name:1,_id:0}).hint({gender:1,user_name:1})
>db.users.find({gender:"M"},{user_name:1,_id:0}).hint({gender:1,user_name:1}).explain()
{
"_id":1,
"product_name": "Samsung S3",
"category": "mobiles",
"product_total": 5,
"product_available": 3,
"product_bought_by": [
{
"customer": "john",
"date": "7-Jan-2014"
},
{
"customer": "mark",
"date": "8-Jan-2014"
}
]
}
In this document, we have embedded the information of the customer who buys the product in the
product_bought_by field. Now, whenever a new customer buys the product, we will first check if the
product is still available using product_available field. If available, we will reduce the value of
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 49/61
03/08/2017 MongoDB Quick Guide
product_available field as well as insert the new customer's embedded document in the product_bought_by
field. We will use findAndModify command for this functionality because it searches and updates the
document in the same go.
>db.products.findAndModify({
query:{_id:2,product_available:{$gt:0}},
update:{
$inc:{product_available:-1},
$push:{product_bought_by:{customer:"rob",date:"9-Jan-2014"}}
}
})
Our approach of embedded document and using findAndModify query makes sure that the product purchase
information is updated only if it the product is available. And the whole of this transaction being in the same
query, is atomic.
In contrast to this, consider the scenario where we may have kept the product availability and the information
on who has bought the product, separately. In this case, we will first check if the product is available using the
first query. Then in the second query we will update the purchase information. However, it is possible that
between the executions of these two queries, some other user has purchased the product and it is no more
available. Without knowing this, our second query will update the purchase information based on the result of
our first query. This will make the database inconsistent because we have sold a product which is not
available.
{
"address": {
"city": "Los Angeles",
"state": "California",
"pincode": "123"
},
"tags": [
"music",
"cricket",
"blogs"
],
"name": "Tom Benzamin"
}
Creating an index on array in turn creates separate index entries for each of its fields. So in our case when we
create an index on tags array, separate indexes will be created for its values music, cricket and blogs.
>db.users.ensureIndex({"tags":1})
After creating the index, we can search on the tags field of the collection like this
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 50/61
03/08/2017 MongoDB Quick Guide
>db.users.find({tags:"cricket"})
To verify that proper indexing is used, use the following explain command
>db.users.find({tags:"cricket"}).explain()
The above command resulted in "cursor" : "BtreeCursor tags_1" which confirms that proper indexing is used.
For creating an index on all the three fields of the sub-document, use the following code
>db.users.ensureIndex({"address.city":1,"address.state":1,"address.pincode":1})
Once the index is created, we can search for any of the sub-document fields utilizing this index as follows
>db.users.find({"address.city":"Los Angeles"})
Remember that the query expression has to follow the order of the index specified. So the index created above
would support the following queries
>db.users.find({"address.city":"Los Angeles","address.state":"California"})
>db.users.find({"address.city":"LosAngeles","address.state":"California",
"address.pincode":"123"})
Extra Overhead
Every index occupies some space as well as causes an overhead on each insert, update and delete. So if you
rarely use your collection for read operations, it makes sense not to use indexes.
RAM Usage
Since indexes are stored in RAM, you should make sure that the total size of the index does not exceed the
RAM limit. If the total size increases the RAM size, it will start deleting some indexes, causing performance
loss.
Query Limitations
Indexing can't be used in queries which use
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 51/61
03/08/2017 MongoDB Quick Guide
Hence, it is always advisable to check the index usage for your queries.
Maximum Ranges
A collection cannot have more than 64 indexes.
The length of the index name cannot be longer than 125 characters.
A compound index can have maximum 31 fields indexed.
MONGODB - OBJECTID
We have been using MongoDB Object Id in all the previous chapters. In this chapter, we will understand the
structure of ObjectId.
The first 4 bytes representing the seconds since the unix epoch
The next 3 bytes are the machine identifier
The next 2 bytes consists of process id
The last 3 bytes are a random counter value
MongoDB uses ObjectIds as the default value of _id field of each document, which is generated while the
creation of any document. The complex combination of ObjectId makes all the _id fields unique.
>newObjectId = ObjectId()
ObjectId("5349b4ddd2781d08c09890f3")
Instead of MongoDB generating the ObjectId, you can also provide a 12-byte id
>myObjectId = ObjectId("5349b4ddd2781d08c09890f4")
>ObjectId("5349b4ddd2781d08c09890f4").getTimestamp()
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 52/61
03/08/2017 MongoDB Quick Guide
This will return the creation time of this document in ISO date format
ISODate("2014-04-12T21:49:17Z")
>newObjectId.str
The above code will return the string format of the Guid
5349b4ddd2781d08c09890f3
MapReduce Command
Following is the syntax of the basic mapReduce command
>db.collection.mapReduce(
function() {emit(key,value);}, //map function
function(key,values) {return reduceFunction}, { //reduce function
out: collection,
query: document,
sort: document,
limit: number
}
)
The map-reduce function first queries the collection, then maps the result documents to emit key-value pairs,
which is then reduced based on the keys that have multiple values.
map is a javascript function that maps a value with a key and emits a key-value pair
reduce is a javascript function that reduces or groups all the documents having the same key
Using MapReduce
Consider the following document structure storing user posts. The document stores user_name of the user
and the status of post.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 53/61
03/08/2017 MongoDB Quick Guide
{
"post_text": "tutorialspoint is an awesome website for tutorials",
"user_name": "mark",
"status":"active"
}
Now, we will use a mapReduce function on our posts collection to select all the active posts, group them on
the basis of user_name and then count the number of posts by each user using the following code
>db.posts.mapReduce(
function() { emit(this.user_id,1); },
{
"result" : "post_total",
"timeMillis" : 9,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 2,
"output" : 2
},
"ok" : 1,
}
The result shows that a total of 4 documents matched the query status :" active " , the map function emitted 4
documents with key-value pairs and finally the reduce function grouped mapped documents having the same
keys into 2.
To see the result of this mapReduce query, use the find operator
>db.posts.mapReduce(
function() { emit(this.user_id,1); },
function(key, values) {return Array.sum(values)}, {
query:{status:"active"},
out:"post_total"
}
).find()
The above query gives the following result which indicates that both users tom and mark have two posts in
active states
In a similar manner, MapReduce queries can be used to construct large complex aggregation queries. The use
of custom Javascript functions make use of MapReduce which is very flexible and powerful.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 54/61
03/08/2017 MongoDB Quick Guide
Starting from version 2.4, MongoDB started supporting text indexes to search inside string content. The Text
Search uses stemming techniques to look for specified words in the string fields by dropping stemming stop
words like a, an, the, etc. At present, MongoDB supports around 15 languages.
>db.adminCommand({setParameter:true,textSearchEnabled:true})
{
"post_text": "enjoy the mongodb articles on tutorialspoint",
"tags": [
"mongodb",
"tutorialspoint"
]
}
We will create a text index on post_text field so that we can search inside our posts' text
>db.posts.ensureIndex({post_text:"text"})
>db.posts.find({$text:{$search:"tutorialspoint"}})
The above command returned the following result documents having the word tutorialspoint in their post
text
{
"_id" : ObjectId("53493d14d852429c10000002"),
"post_text" : "enjoy the mongodb articles on tutorialspoint",
"tags" : [ "mongodb", "tutorialspoint" ]
}
{
"_id" : ObjectId("53493d1fd852429c10000003"),
"post_text" : "writing tutorials on mongodb",
"tags" : [ "mongodb", "tutorial" ]
}
If you are using old versions of MongoDB, you have to use the following command
Using Text Search highly improves the search efficiency as compared to normal search.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 55/61
03/08/2017 MongoDB Quick Guide
>db.posts.getIndexes()
After getting the name of your index from above query, run the following command. Here, post_text_text is
the name of the index.
>db.posts.dropIndex("post_text_text")
Unlike text search, we do not need to do any configuration or command to use regular expressions.
Consider the following document structure under posts collection containing the post text and its tags
{
"post_text": "enjoy the mongodb articles on tutorialspoint",
"tags": [
"mongodb",
"tutorialspoint"
]
}
>db.posts.find({post_text:{$regex:"tutorialspoint"}})
>db.posts.find({post_text:/tutorialspoint/})
>db.posts.find({post_text:{$regex:"tutorialspoint",$options:"$i"}})
One of the results returned from this query is the following document which contains the word
tutorialspoint in different cases
{
"_id" : ObjectId("53493d37d852429c10000004"),
"post_text" : "hey! this is my post on TutorialsPoint",
"tags" : [ "tutorialspoint" ]
}
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 56/61
03/08/2017 MongoDB Quick Guide
>db.posts.find({tags:{$regex:"tutorial"}})
If the regular expression is a prefix expression, all the matches are meant to start with a certain
string characters. For e.g., if the regex expression is ^tut, then the query has to search for only those
strings that begin with tut.
Downloading RockMongo
You can download the latest version of RockMongo from here: http://rockmongo.com/downloads
Installing RockMongo
Once downloaded, you can unzip the package in your server root folder and rename the extracted folder to
rockmongo. Open any web browser and access the index.php page from the folder rockmongo. Enter
admin/admin as username/password respectively.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 57/61
03/08/2017 MongoDB Quick Guide
Export/Import Data
To import/export data of any collection, click on that collection and then click on Export/Import link on the
top panel. Follow the next instructions to export your data in a zip format and then import the same zip file to
import back data.
MONGODB - GRIDFS
GridFS is the MongoDB specification for storing and retrieving large files such as images, audio files, video
files, etc. It is kind of a file system to store files but its data is stored within MongoDB collections. GridFS has
the capability to store files even greater than its document size limit of 16MB.
GridFS divides a file into chunks and stores each chunk of data in a separate document, each of maximum size
255k.
GridFS by default uses two collections fs.files and fs.chunks to store the file's metadata and the chunks.
Each chunk is identified by its unique _id ObjectId field. The fs.files severs as a parent document. The
files_id field in the fs.chunks document links the chunk to its parent.
{
"filename": "test.txt",
"chunkSize": NumberInt(261120),
"uploadDate": ISODate("2014-04-13T11:32:33.557Z"),
"md5": "7b762939321e146569b07f72c62cca4f",
"length": NumberInt(646)
}
The document specifies the file name, chunk size, uploaded date, and length.
{
"files_id": ObjectId("534a75d19f54bfec8a2fe44b"),
"n": NumberInt(0),
"data": "Mongo Binary Data"
}
Open your command prompt, navigate to the mongofiles.exe in the bin folder of MongoDB installation folder
and type the following code
Here, gridfs is the name of the database in which the file will be stored. If the database is not present,
MongoDB will automatically create a new document on the fly. Song.mp3 is the name of the file uploaded. To
see the file's document in database, you can use find query
>db.fs.files.find()
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 58/61
03/08/2017 MongoDB Quick Guide
{
_id: ObjectId('534a811bf8b4aa4d33fdf94d'),
filename: "song.mp3",
chunkSize: 261120,
uploadDate: new Date(1397391643474), md5: "e4f53379c909f7bed2e9d631e15c1c41",
length: 10401959
}
We can also see all the chunks present in fs.chunks collection related to the stored file with the following code,
using the document id returned in the previous query
>db.fs.chunks.find({files_id:ObjectId('534a811bf8b4aa4d33fdf94d')})
In my case, the query returned 40 documents meaning that the whole mp3 document was divided in 40
chunks of data.
Capped collections restrict updates to the documents if the update results in increased document size. Since
capped collections store documents in the order of the disk storage, it ensures that the document size does not
increase the size allocated on the disk. Capped collections are best for storing log information, cache data, or
any other high volume data.
>db.createCollection("cappedLogCollection",{capped:true,size:10000})
In addition to collection size, we can also limit the number of documents in the collection using the max
parameter
>db.createCollection("cappedLogCollection",{capped:true,size:10000,max:1000})
If you want to check whether a collection is capped or not, use the following isCapped command
>db.cappedLogCollection.isCapped()
If there is an existing collection which you are planning to convert to capped, you can do it with the following
code
>db.runCommand({"convertToCapped":"posts",size:10000})
This code would convert our existing collection posts to a capped collection.
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 59/61
03/08/2017 MongoDB Quick Guide
>db.cappedLogCollection.find().sort({$natural:-1})
There are few other important points regarding capped collections worth knowing
There are no default indexes present in a capped collection, not even on _id field.
While inserting a new document, MongoDB does not have to actually look for a place to accommodate
new document on the disk. It can blindly insert the new document at the tail of the collection. This
makes insert operations in capped collections very fast.
Similarly, while reading documents MongoDB returns the documents in the same order as present on
disk. This makes the read operation very fast.
Since this is not a default feature in MongoDB, we will programmatically achieve this functionality by using a
counters collection as suggested by the MongoDB documentation.
{
"_id":1,
"product_name": "Apple iPhone",
"category": "mobiles"
}
For this, create a counters collection, which will keep track of the last sequence value for all the sequence
fields.
>db.createCollection("counters")
Now, we will insert the following document in the counters collection with productid as its key
{
"_id":"productid",
"sequence_value": 0
}
The field sequence_value keeps track of the last value of the sequence.
Use the following code to insert this sequence document in the counters collection
>db.counters.insert({_id:"productid",sequence_value:0})
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 60/61
03/08/2017 MongoDB Quick Guide
Now, we will create a function getNextSequenceValue which will take the sequence name as its input,
increment the sequence number by 1 and return the updated sequence number. In our case, the sequence
name is productid.
>function getNextSequenceValue(sequenceName){
return sequenceDocument.sequence_value;
}
>db.products.insert({
"_id":getNextSequenceValue("productid"),
"product_name":"Apple iPhone",
"category":"mobiles"
})
>db.products.insert({
"_id":getNextSequenceValue("productid"),
"product_name":"Samsung S3",
"category":"mobiles"
})
As you can see, we have used the getNextSequenceValue function to set value for the _id field.
To verify the functionality, let us fetch the documents using find command
>db.products.find()
The above query returned the following documents having the auto-incremented _id field
https://www.tutorialspoint.com/cgi-bin/printpage.cgi 61/61