Professional Documents
Culture Documents
Mongo DB
Mongo DB
BEGINNER MODULE
1. Introduction to MongoDB
Introduction
MongoDB is an open source database that uses a document-oriented data model. MongoDB
is one of several database types to arise in the mid-2000s under the NoSQL banner. Instead
of using tables and rows as in relational databases, MongoDB is built on an architecture of
collections and documents.
NoSQL means Not Only SQL, implying that when designing a software solution or product,
there is more than one storage mechanism that could be used based on the needs. NoSQL
was a hashtag (#nosql) chosen for a meetup to discuss these new databases. The most
important result of the rise of NoSQL is Polyglot Persistence. NoSQL does not have a
prescriptive definition but we can make a set of common observations, such as:
Document Database¶
A record in MongoDB is a document, which is a data structure composed of field and value
pairs. Unlike relational databases where data is stored in the format of rows and columns
,MongoDB stores the data as JSON objects. The values of fields may include other
documents, arrays, and arrays of documents.
Notice there are two different documents (separated by "."). Storing data in this fashion is
called as document oriented database. MongoDb is a document oriented database.
If we have to store the same data in any relation db,the schema of the database may look
like this
1
Schema Free
MongoDB is a JSON-style data store. The documents stored in the database can have
varying sets of fields, with different types for each field. One could have the following objects
in a single collection:{name : “Joe”, x : 3.3, y : [1,2,3]}{name : “Kate”, x : “abc”}{q : 456}One of
the great benefits of thesedynamic objects is that schema migrations become very easy.
With a traditional RDBMS, releases of code might contain data migration scripts. Further,
each release should have a reverse migration script in case a rollback is necessary. ALTER
TABLE operations can be very slow and result in scheduled downtime. With a schemaless
database, 90% of the time adjustments to the database become transparent and automatic.
For example, if we wish to add GPA to the student objects, we add the attribute, resave, and
all is well – if we look up an existing student and reference GPA, we just get back null.
Further, if we roll back our code, the new GPA fields in the existing objects are unlikely to
cause problems if our code was well written.
Sharding
Sharding is the process of storing data records across multiple machines and it is MongoDB's
approach to meet 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.
Mongo DB VS RDBMS
Many concepts in MySQL have close analogs in MongoDB. This table outlines some of the
common concepts in each system.
2
MySQL MongoDB
Table Collection
Row Document
Column Field
Query Language
Both MySQL and MongoDB have a rich query language. A comprehensive list of statements
can be found in theMongoDB documentation.
MySQL
INSERT INTO users (user_id, age, status) VALUES ("bcd001", 45, "A")
MongoDB
db.users.insert({
user_id: "bcd001",
age: 45,
status: "A"})
MongoDB Installation
PLATFORM SUPPORT :
Starting in version 2.2, MongoDB does not support Windows XP. Please use a more recent
version of Windows to use more recent releases of MongoDB.
IMPORTANT :
If you are running any edition of Windows Server 2008 R2 or Windows 7, please install a
hotfix to resolve an issue with memory-mapped files on Windows.
3
Requirements
MongoDB Community Edition requires Windows Server 2008 R2, Windows Vista, or later. The
.msiinstaller includes all other software dependencies and will automatically upgrade any
older version of MongoDB installed using an .msi file.
NOTE
To install a version of MongoDB prior to 3.2, please refer to that version’s documentation.
For example, see version 3.0.
MongoDB for Windows 64-bit runs only on Windows Server 2008 R2, Windows 7 64-bit,
and newer versions of Windows. This build takes advantage of recent enhancements to the
Windows Platform and cannot operate on older versions of Windows.
MongoDB for Windows 64-bit Legacy runs on Windows Vista, and Windows Server 2008
and does not include recent performance enhancements.
To find which version of Windows you are running, enter the following commands in
the Command Prompt or Powershell:
Download the latest production release of MongoDB from the MongoDB downloads page.
Ensure you download the correct version of MongoDB for your Windows system. The 64-bit
versions of MongoDB do not work with 32-bit Windows.
Interactive Installation
You may specify an installation directory if you choose the “Custom” installation option.
NOTE
4
These instructions assume that you have installed MongoDB to C:\mongodb.
MongoDB is self-contained and does not have any other system dependencies. You can run
MongoDB from any folder you choose. You may install MongoDB in any folder
(e.g. D:\test\mongodb).
Unattended Installation
You may install MongoDB Community unattended on Windows from the command line
usingmsiexec.exe.
Change to the directory containing the .msi installation binary of your choice and invoke:
msiexec.exe /q /i mongodb-win32-x86_64-2008plus-ssl-3.2.5-signed.msi ^
INSTALLLOCATION="C:\mongodb" ^ ADDLOCAL="all"
You can specify the installation location for the executable by modifying
the INSTALLLOCATION value.
By default, this method installs all MongoDB binaries. To install specific MongoDB
component sets, you can specify them in the ADDLOCAL argument using a comma-
separated list including one or more of the following component sets:
Server mongod.exe
Router mongos.exe
Client mongo.exe
MonitoringTools mongostat.exe, mongotop.exe
ImportExportTools mongodump.exe, mongorestore.exe, mongo
export.exe, mongoimport.exe
bsondump.exe, mongofiles.exe, mongooplog
MiscellaneousTools
.exe, mongoperf.exe
5
For instance, to install only the MongoDB utilities, invoke:
ADDLOCAL="MonitoringTools,ImportExportTools,MiscellaneousTools"
WARNING
MongoDB requires a data directory to store all data. MongoDB’s default data directory path
is\data\db. Create this folder using the following commands from a Command Prompt:
md \data\db
You can specify an alternate path for data files using the --dbpath option to mongod.exe, for
example:
If your path includes spaces, enclose the entire path in double quotes, for example:
Start MongoDB.
C:\mongodb\bin\mongod.exe
Depending on the security level of your system, Windows may pop up a Security Alert dialog
box about blocking “some features” of C:\mongodb\bin\mongod.exe from communicating
on networks. All users should select Private Networks, such as my home or work network and
click Allow access. For additional information on security and MongoDB, please see
theSecurity Documentation.
6
Connect to MongoDB.
C:\mongodb\bin\ mongo.exe
If you want to develop applications using .NET, see the documentation of C# and
MongoDB for more information.
To help you start using MongoDB, MongoDB provides Getting Started Guides in various
driver editions. See Getting Started for the available editions.
Create directories.
IMPORTANT
7
Run all of the following commands in Command Prompt with “Administrative Privileges”.
"C:\mongodb\bin\mongod.exe" --remove
The following procedure assumes you have installed MongoDB Community using
the .msi installer with the path C:\mongodb\.
If you have installed in an alternative directory, you will need to adjust the paths as
appropriate.
8
Create directories.
Create directories for your database and log files:
sc.exe requires a space between “=” and the configuration values (eg “binPath=”), and a “\”
to escape double quotes.
To remove the MongoDB service, first stop the service and then run the following command:
Trouble Shooting
1.I am getting the following error in Mac when I try to start mongodb
9
Solution:
In this section,we are going to see how to set the environment variables for mongodb .
If we don’t set it each time we have to launch the mongodb from the install location of the
mongodb
Once the environment variable has been set.mongod can be invoked directly from command
prompt
10
Windows 10 and Windows 8
In the Edit System Variable window, specify the value of the PATH environment variable.
Add the location of the bin folder of the mongodb in PATH variable
Click Ok
Click OK again
After setting the environment variable now we can invoke mongo db from command prompt
Default
Description
Port
The default port for mongod and mongos instances. You can
27017
change this port with port or --port.
MongoChef is the best tool to work with MongoDB. Whether you’re exploring your local
database or working with shards and replica sets, MongoChef is here to help and make
working with MongoDB a pleasure.
You can install the version of MongoChef based on your OS .Download Mongochef from
below link
http://3t.io/mongochef/
Go to your <mongodb installation dir>:
11
Type ./bin/mongo to start mongo:
./bin/mongo
Syntax:
Example:
If you want to check your databases list, then use the command show dbs.
Your created database (mydb) is not present in the list. To display database, you need
to insert at least one document into it.
>db.myCollection.insert({"name":"guvi"})
12
test 0.23012GB
In mongodb default database is test database. If you didnt ' create any database then
collections will be stored in the test database.
TroubleShooting
Solution: You need to create at least one document in database, after you database will
show up in list.
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 available databases by using the command show dbs
13
mydb 0.23012GB
>db.dropDatabase()
4. Creating Collections
Syntax
Paramete
Type Description
r
Name String Name of the collection to be created
Documen (Optional) Specify options about memory size and
Options
t indexing
The options parameter is optional, so you need to specify the only name of the collection.
Following is the list of options you can use:
14
automatically overwrites its oldest entries when it
reaches its maximum size. If you specify true, you
need to specify size parameter also.
autoIndexI (Optional) If true, automatically create the index on a
Boolean
D _id field.s Default value is false.
(Optional) Specifies a maximum size in bytes for a
size number capped collection. If capped is true, then you need to
specify this field also.
(Optional) Specifies the maximum number of
max number
documents allowed in the capped collection.
While inserting the document, MongoDB first checks size field of capped collection, then it
checks max field.
Examples:
>db.createCollection("mycollection")
{"ok" : 1}
You can check the created collection by using the command show collections
>show collections
mycollection
system.indexes
{"ok" : 1}
>db.guvi.insert({"name" : "tutorialspoint"})
>show collections
mycol
15
mycollection
guvi
Drop Collection
Syntax:
db.COLLECTION_NAME.drop()
Example:
>show collections
mycol
mycollection
>db.mycollection.drop()
true
mycol
drop() method will return true if the selected collection is dropped successfully otherwise it
will return false
INTERMEDIATE MODULE
16
To insert data into MongoDB collection, you need to use
MongoDB's insert()or save()method.
Syntax
>db.COLLECTION_NAME.insert(document)
Example
Here mycol is the collection name. If the collection doesn't exist in the database, then
MongoDB will create this collection and then insert 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 the single query, you can pass an array of documents
in the insert() command.
Example
17
url: 'https://www.guvi.in,
2. Query Document
Syntax
>db.COLLECTION_NAME.find()
18
To display the results in a formatted way, you can use pretty() method.
Syntax
>db.mycol.find().pretty()
Example
>db.mycol.find().pretty()
{
Apart from find() method there is findOne() method, that returns only one document.
"
To query the document on the basis of some condition, you can use following operations
AND in MongoDB
Syntax
Example
19
Below given example will show all the tutorials written by 'guvi' and whose title is
'MongoDB Overview'
{ "_id": ObjectId(7df78ad8902c),
"likes": "100"}
For the above given example equivalent where clause will be ' where by=‘guvi’ AND
title='MongoDB Overview' '. You can pass any number of key, value pairs in find clause.
OR in MongoDB
Syntax
>db.mycol.find(
Example
Below given example will show all the tutorials written by 'tutorials point' or whose
title is 'MongoDB Overview'
20
"url": "https://www.guvi.in",
Example
Below given example will show the documents that have likes greater than 100 and
whose title is either 'MongoDB Overview' or by is 'tutorials point'. Equivalent sql
where clause is 'where likes>10 AND (by='tutorials point' OR title='MongoDB
Overview')'
{
Syntax
21
>db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)
Example
Following example will set the new title 'New MongoDB Tutorial' of the documents
whose title is 'MongoDB Overview'
>db.mycol.find()
By default mongodb will update only single document, to update multiple you need
to set a paramter 'multi' to true.
The save() method replaces the existing document with the new document passed in save()
method
Syntax
>db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})
Example
Following example will replace the document with the _id '5983548781331adf45ec7'
22
>db.mycol.find(){"_id" : ObjectId(5983548781331adf45ec5), "title":"guvi New
Topic", "by":"guvi"}{"_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{"_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point
Overview"}
ADVANCED MODULE
1. Delete Document
Syntax:
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
Example
Following example will remove all the documents whose title is 'MongoDB Overview'
>db.mycol.remove({'title':'MongoDB Overview'})
>db.mycol.find()
If there are multiple records and you want to delete only first record, then
setjustOne parameter in remove() method
23
>db.COLLECTION_NAME.remove(DELETION_CRITERIA,1)
If you don't specify deletion criteria, then mongodb will delete whole documents
from the collection. This is equivalent of SQL's truncate command.
>db.mycol.remove()
>db.mycol.find()
2. MongoDB Projection
In MongoDB projection, meaning is selecting only 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 quering the document.
>db.mycol.find({},{"title":1,_id:0})
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}
24
{"title":"Database 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
Syntax:
>db.COLLECTION_NAME.find().sort({KEY:1})
Example
Following example will display the documents sorted by title in descending order.
>db.mycol.find({},{"title":1,_id:0}).sort({"title":-1})
{"title":"NoSQL Overview"}
{"title":"MongoDB Overview"}
{"title":"Database Overview"}
Please note if you don't specify the sorting preference, then sort() method will display
documents in ascending order.
Syntax:
25
Basic syntax of limit() method is as follows
>db.COLLECTION_NAME.find().limit(NUMBER)
Example
Following example will display only 2 documents while quering the document.
>db.mycol.find({},{"title":1,_id:0}).limit(2)
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}>
If you don't specify number argument in limit() method then it will display all documents
from the collection.
Apart from limit() method, there is one more method skip() which also accepts number type
argument and used to skip a number of documents.
Syntax:
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
Example:
>db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1)
{"title":"NoSQL Overview"}>
EXPERT MODULE
26
1. 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 requires the 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 an index.
Syntax:
>db.COLLECTION_NAME.ensureIndex({KEY:1})
Here the key is the name of filed on which you want to create index and 1 are for ascending
order. To create an index in descending order you need to use -1.
Example
>db.mycol.ensureIndex({"title":1}) >
>db.mycol.ensureIndex({"title":1,"description":-1}) >
ensureIndex() method also accepts list of options (which are optional), whose list is
given below
27
concatenating the names of the indexed fields
and the sort order.
Creates a unique index on a field that may
have duplicates. MongoDB indexes only the
first occurrence of a key and removes all
dropDups Boolean documents from the collection that contain
subsequent occurrences of that key. Specify
true to create a unique index. The default
value is false.
If true, the index only references documents
with the specified field. These indexes use less
sparse Boolean space but behave differently in some
situations (particularly sorts). The default
value is false.
Specifies a value, in seconds, as a TTL to
expireAfterSecond
Integer control how long MongoDB retains
s
documents in this collection.
The index version number. The default index
index
v version depends on the version of MongoDB
version
running when creating the index.
The weight is a number ranging from 1 to
99,999 and denotes the significance of the
weights document
field relative to the other indexed fields in
terms of the score.
For a text index, the language that determines
the list of stop words and the rules for the
default_language string
stemmer and tokenizer. The default value is
English.
For a text index, specify the name of the field
in the document that contains, the language
language_override string
to override the default language. The default
value is language.
2. 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 the group
by is an equivalent of mongodb aggregation.
Syntax:
28
Basic syntax of aggregate() method is as follows
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Example:
{id: ObjectId(7df78ad8902c),
likes: 100},
{_id: ObjectId(7df78ad8902d),
by_user: 'guvi’,
likes: 10},
29
likes: 750},
Now from the above collection if you want to display a list that how many tutorials are
written by each user then you will use aggregate() method as shown below:
Sql equivalent query for the above use case will be select by_user, count(*) from mycol group
by by_user
Expressio
Description Example
n
Sums up the defined value db.mycol.aggregate([{$group :
$sum from all documents in the {_id : "$by_user", num_tutorial :
collection. {$sum : "$likes"}}}])
Calculates the average of all db.mycol.aggregate([{$group :
$avg given values from all {_id : "$by_user", num_tutorial :
documents in the collection. {$avg : "$likes"}}}])
Gets the minimum of the db.mycol.aggregate([{$group :
$min corresponding values from all {_id : "$by_user", num_tutorial :
documents in the collection. {$min : "$likes"}}}])
Gets the maximum of the db.mycol.aggregate([{$group :
$max corresponding values from all {_id : "$by_user", num_tutorial :
documents in the collection. {$max : "$likes"}}}])
db.mycol.aggregate([{$group :
Inserts the value to an array in
$push {_id : "$by_user", url :{$push:
the resulting document.
"$url"}}}])
Inserts the value to an array in db.mycol.aggregate([{$group :
$addToSe
the resulting document but {_id : "$by_user", url :{$addToSet :
t
does not create duplicates. "$url"}}}])
Gets the first document from
the source documents
according to the grouping. db.mycol.aggregate([{$group :
$first Typically this makes only {_id : "$by_user", first_url :{$first :
sense together with some "$url"}}}])
previously applied “$sort”-
stage.
$last Gets the last document from db.mycol.aggregate([{$group :
30
the source documents
according to the grouping.
Typically this makes only {_id : "$by_user", last_url :{$last :
sense together with some "$url"}}}])
previously applied “$sort”-
stage.
Syntax:
>mongodump
Example
Start your mongod server. Assuming that your mongod server is running on localhost and
port 27017. Now open a command prompt and go to 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 port27017 and back all data
of the server to directory /bin/dump/. Output of the command is shown below:
31
There are a list of available options that can be used with the mongodumpcommand.
Restore data
Syntax
>mongorestore
32
33