Mongo DB
Mongo DB
mongoDB
Lab Manual
mongoDB
• Document-oriented NoSQL database.
• Schema-free.
• Based on Binary JSON; BSON.
• Organized in Group of Documents
• Collections
• Informal name spacing
• Auto-Sharding in order to scale horizontally.
• Simple query language. Rich, document-based
queries.
• Map/Reduce support.
• Open Source (GNU AGPL v3.0.)
2
1
2/22/2018
JSON; BSON
• JSON (JavaScript Object Notation) is a lightweight data-
interchange format.
• BSON /ˈbiːsən/ is a computer data interchange format
used mainly as a data storage and network transfer format
in the MongoDB database.
Sharding
• Sharding is a type of database
partitioning that separates very large
databases the into smaller, faster, more
easily managed parts called data shards. The
word shard means a small part of a whole.
• Tables are divided and distributed into
multiple servers.
• The total number of rows in each table in
each database is reduced.
• Improves search performance.
4
2
2/22/2018
Map-reduce
• Map-reduce is a data processing paradigm
for condensing large volumes of data into
useful aggregated results.
• For map-reduce operations, MongoDB
provides the mapReduce database
command.
Relationship of RDBMS
terminology with MongoDB
RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Table Join Embedded Documents
Primary Key (Default key _id provided
Primary Key by mongodb itself)
3
2/22/2018
MongoDB Blocks
4
2/22/2018
Syntax : db.dropDatabase()
Ex:
>use College
switched to db College
>db.dropDatabase()
>{ "dropped" : "College", "ok" : 1 }
>
9
Syntax :
db.createCollection(name, options)
Ex:
>db.createCollection("Student")
10
5
2/22/2018
Syntax :
db.COLLECTION_NAME.drop()
Ex:
>db.Student.drop()
11
Miscellaneous Commands
>db To check your currently selected
database.
12
6
2/22/2018
Data Types
Data Type Description
To store the data. String in MongoDB must be
String UTF-8 valid.
To store a numerical value. Integer can be 32
Integer bit or 64 bit depending upon your server.
Boolean To store a Boolean (true/ false) value.
Double To store floating point values.
Min/Max To compare a value against the lowest and
Keys highest BSON elements.
To store arrays or list or multiple values into
Arrays one key.
This can be handy for recording when a
Timestamp document has been modified or added.
This datatype is used for embedded
Object documents.
13
7
2/22/2018
MongoDB Operations
users
15
Syntax of insert():
>db.COLLECTION_NAME.insert(document)
16
8
2/22/2018
MongoDB
17
18
9
2/22/2018
19
10
2/22/2018
Ex 1: >db.Student.find()
Ex 2: >db.Student.find({sid:"1004"})
22
11
2/22/2018
23
Array elements
• The following syntax allows us to apply a
condition on a required element of an
array.
Syntax:
db.COLLECTION.find( { “ARRAY-NAME.
INDEX-NUMBER": “VALUE" } )
Ex :
db.Student.find( { "courses.0": "WT" } )
24
12
2/22/2018
RDBMS
Operation Syntax Example
Equivalent
where
Equality {<key>:<value>} db.Student.find({"address":"Muscat"}).pretty()
address="Muscat"
Less than or
{<key>:{$lte:<value >}} db.Student.find({"age":{$lte:25}}).pretty() where age<=25
Equal
Greater than
{<key>:{$gte:<value >}} db.Student.find({"age":{$gte:25}}).pretty() where age>=25
or Equal
25
26
13
2/22/2018
Update the users of age greater than 18 by setting the status field to A.
SQL
MongoDB
27
SQL
MongoDB
28
14
2/22/2018
UPDATE users SET age = age + 3 db.users.update( { status: "A" } , { $inc: { age: 3
WHERE status = "A" } }, { multi: true })
30
15
2/22/2018
AND in MongoDB
• In the find() method, if you pass multiple
keys by separating them by ',' then
MongoDB treats it as AND condition.
Syntax:
>db.mycol.find({key1:value1,
key2:value2}).pretty()
Ex :
>db.Student.find({age:29,address:"Ibra"}).pretty()
31
OR in MongoDB
• To query documents based on the OR
condition, you need to use $or keyword.
Syntax:
>db.mycol.find(
{
$or: [ {key1: value1}, {key2:value2} ]
} ).pretty()
Ex :
>db.Student.find({$or:[{age:29},{address:"Ibra"}]}).pretty()
32
16
2/22/2018
33
34
17
2/22/2018
35
Ex:
>db.Student.update({address:"Ibri"},{$set:{ad
dress:"Musanna"}} ,{multi:true})
36
18
2/22/2018
19
2/22/2018
Ex :
>db.Student.remove({"Address":"Ibra"})
39
Syntax:
>db.COLLECTION_NAME.remove(DELETIO
N_CRITERIA,1)
40
20
2/22/2018
Syntax:
>db.COLLECTION-NAME.remove()
41
MongoDB - Projection
• Projection can allows us to filter the
Collection Vertically.
• You can select the required columns of a
collection using the second optional
parameter [1 or 0]of find() method.
• 1 is used to show the field.
• 0 is used to hide the fields
Syntax:
>db.COLLECTION_NAME.find({},{KEY:1})
42
21
2/22/2018
Ex 2:
>db.Student.find({address:"Musanna"},{name:
1, address:1, _id:0})
43
44
22
2/22/2018
Syntax:
>db.COLLECTION_NAME.find().sort({KEY:1})
Ex :
> db.Student.find({},{name:1,_id:0}).sort({name:1})
45
MongoDB - Indexing
The ensureIndex() Method: To create an index
you need to use ensureIndex() method of
MongoDB.
Here key is the name of the file on which you want
to create index and 1 is for ascending order. To
create index in descending order you need to use -1.
Syntax:
>db.COLLECTION_NAME.ensureIndex({KEY:1})
Ex 1: >db.Student. ensureIndex({name:1})
Ex 2: >db.Student. ensureIndex({name:1, address:-1})
46
23