An introduction to MongoDB
Big Data Every Where!
• Lots of data is being collected
and warehoused
– Web data, e-commerce
– purchases at department/
grocery stores
– Bank/Credit Card
transactions
– Social Network
How much data?
• Google processes 20 PB a day (2008)
• Wayback Machine has 3 PB + 100 TB/month (3/2009)
• Facebook has 2.5 PB of user data + 15 TB/day (4/2009)
• eBay has 6.5 PB of user data + 50 TB/day (5/2009)
• CERN’s Large Hydron Collider (LHC) generates 15 PB a
year
640K ought to be
enough for anybody.
Why mongoDB?
• https://www.youtube.com/watch?v=CvIr-2lMLsk
2013
In Production
http://www.mongodb.org/about/production-deployments/
NoSQL
• Key-value
• Graph database
• Document-oriented
• Column family
6
BASE Properties
• Basic Availability
– The database appears to work most of the time.
• Soft-state
– Stores don’t have to be write-consistent, nor do
different replicas have to be mutually consistent all
the time.
• Eventual consistency
– Stores exhibit consistency at some later point (e.g.,
lazily at read time).
What Is mongoDB?
A document-oriented database
documents encapsulate and encode data (or
information) in some standard formats or encodings
NoSQL database
uses BSON format
schema-less
No more configuring database columns with types
No transactions
No joins
9
SQL Vs MongoDB
SQL Concepts MongoDB Concepts
database database
table Collection
Row Document 0r BSON Document
Column Field
Index Index
Table Join Embedded documents & Linking
Primary key Primary Key
Specify any unique column or column In MongoDB, the primary key is
combination as primary key. automatically set to the _id field.
aggregation (e.g. group by) aggregation pipeline
Executables
Oracle MySQL MongoDB
Database Server oracle mysqld mongod
sqlplus mongo
Database Client mysql
• MongoDB Vs Relational DBMS
• Collection vs table
• Document vs row
• Field vs column
• schema-less vs schema-oriented
13
The Basics
• A MongoDB instance may have zero or more databases
• A database may have zero or more collections.
– Can be thought of as the relation (table) in DBMS, but with
many differences.
• A collection may have zero or more documents.
– Docs in the same collection don’t even need to have the
same fields
– Docs are the records in RDBMS
– Docs can embed other documents
– Documents are addressed in the database via a unique key
14
•
Continued..
• A document may have one or more fields.
2013
• MongoDB Indexes is much like their RDBMS counterparts.
Table Creation in Oracle
CREATE TABLE Teacher_Info( Teacher_id Varchar(10), Teacher_Name
Varchar(10), Dept_Name Varchar(10), Salary Number(10),Status
char(1), PRIMARY KEY (id) );
INSERT INTO TeacherInfo(Teacher_id, Teacher_Name, Dept_Name, Salary,
Status) VALUES (“Pic001", “Ravi”, “IT”,30000, “A");
Output: Teacher_Info
Teacher_id Teacher_Na Dept_Name, Salary Status
me
Pic001 Ravi IT 30000 A
Pic002 Mangesh IT 20000 A
Pic003 Akshay Comp 25000 N
• Example: Mongo Document
Teacher_info = {
Teacher_id: “Pic001",
Teacher_Name: “Ravi",
Dept_Name: “IT”,
Sal:30000,
status: "A"
}
17
Insert Command:
db.Teacher_info.insert( { Teacher_id: “Pic001", Teacher_Name:
“Ravi",Dept_Name: “IT”, Sal:30000, status: "A" } )
db.Teacher_info.insert( { Teacher_id: “Pic002", Teacher_Name:
“Ravi",Dept_Name: “IT”, Sal:20000, status: "A" } )
db.Teacher_info.insert( { Teacher_id: “Pic003", Teacher_Name:
“Akshay",Dept_Name: “Comp”, Sal:25000, status: “N" } )
Example: Mongo Collection
{ "_id": ObjectId("4efa8d2b7d284dad101e4bc9"),
"Last Name": "DUMONT",
"First Name": "Jean",
"Date of Birth": "01-22-1963" },
{ "_id": ObjectId("4efa8d2b7d284dad101e4bc7"),
"Last Name": "PELLERIN",
"First Name": "Franck",
"Date of Birth": "09-19-1983",
"Address": "1 chemin des Loges",
"City": "VERSAILLES" }
19
Document store
RDBMS MongoDB
Database Database
Table, View Collection
Row Document (JSON, BSON)
Column Field
Index Index
Join Embedded Document
Foreign Key Reference
Partition Shard 20
Document store
RDBMS MongoDB
> db.user.findOne({age:39})
Database Database {
Table, View Collection "_id" : ObjectId("5114e0bd42…"),
"first" : "John",
Row "last" : "Doe",
Document (JSON, BSON)
"age" : 39,
Column Field "interests" : [
Index Index "Reading",
Join Array
Embedded Document
"Mountain Biking ]
Foreign Key Reference "favorites": {
"color": "Blue",
Partition Shard "sport": "Soccer"}
} 21
Embedded document
CRUD
• Create
db.collection.insert( <document> )
db.collection.save( <document> )
db.collection.update( <query>, <update>, { upsert:
true } )
• Read
db.collection.find( <query>, <projection> )
db.collection.findOne( <query>, <projection> )
• Update
db.collection.update( <query>, <update>, <options> )
• Delete 22
db.collection.remove( <query>, <justOne> )
CRUD example
> db.user.find ()
> db.user.insert({ {
first: "John", "_id" : ObjectId("51…"),
last : "Doe", "first" : "John",
age: 39 "last" : "Doe",
}) "age" : 39
}
> db.user.update(
{"_id" : ObjectId("51…")},
{
> db.user.remove({
$set: {
age: 40,
"first": /^J/
salary: 7000} }) 23
}
)
The components of a MongoDB find operation.
The next diagram shows the same query in SQL:
SQL & Mongodb Commands
SQL SELECT Statements MongoDB find() Statements
SELECT * FROM Teacher_info; db.Teacher_info.find()
SELECT * FROM Teacher_info db.Teacher_info.find( {s
WHERE sal = 25000; al: 25000})
SELECT Teacher_id FROM db.Teacher_info.find( {T
Teacher_info WHERE eacher_id: "pic001"})
Teacher_id = 1;
SQL & Mongodb Commands
SELECT * FROM Teacher_info db.Teacher_info.find({status:
WHERE status != "A“; {$ne:"A"}})
SELECT * FROM Teacher_info db.Teacher_info.find({status:"A
WHERE status = "A" AND sal = ", sal:20000})
20000;
SELECT * FROM Teacher_info > db.Teacher_info.find( { $or:
WHERE status = "A" OR sal = [ { status: "A" } ,
50000; { sal:50000 } ] } )
SELECT * FROM Teacher_info db. Teacher_info.find( { sal:
WHERE sal > 40000 { $gt: 40000 } } )
SELECT * FROM db. Teacher_info.find( { sal:
Teacher_infoWHERE sal < { $gt: 30000 } } )
30000
SQL & Mongodb Commands
SELECT * FROM Teacher_info db. Teacher_info.find( { status:
WHERE status = "A" ORDER BY "A" } ).sort( { sal: 1 } )
SAL ASC
SELECT * FROM users WHERE db. Teacher_info.find( { status:
status = "A" ORDER BY SAL "A" } ).sort( {sal: -1 } )
DESC
SELECT COUNT(*) FROM db. Teacher_info.count()
Teacher_info; or
db. Teacher_info.find().count()
SELECT DISTINCT(Dept_name) db.
FROM Teacher_info; Teacher_info.distinct( “Dept_n
ame" )
Update Records
UPDATE Teacher_info SET db. Teacher_info.update( { sal:
Dept_name = “ETC" WHERE sal { $gt: 25000 } }, { $set:
> 250000 { Dept_name: “ETC" } }, { multi:
true } )
UPDATE Teacher_infoSET sal = db.
sal + 10000 WHERE status = "A" Teacher_info.update( { status:
"A" } , { $inc: { sal: 10000 } },
{ multi: true } )
Delete Records
DELETE FROM Teacher_info db.Teacher_info.remove({Tea
WHERE Teacher_id = cher_id: "pic001"});
“pic001"
DELETE FROM Teacher_info; db. Teacher_info.remove({})
Other Commands.. 2013.02.11.
1.Alter Table in Oracle &
MongoDb
Oracle:
ALTER TABLE Teacher_info ADD join_date DATETIME
MongoDb:
At the document level, update ()operations can add fields to
existing documents using the $set operator.
Ex:
db.Teacher_info.update( { }, { $set: { join_date: new Date() } },
{ multi: true} )
2. Drop Command:
Oracle:
DROP TABLE Teacher_info
Mongo:
db.Teacher_info.drop()
3. INSERT A DOCUMENT WITH INSERT()
METHOD
The following statement inserts a document
with three field
db.inventory.insert( { _id: 10, type: "misc", item:
"card", qty: 15 } )
INSERT DOCUMENTS
In MongoDB, the db.collection.insert() method
adds new documents into a collection.
In addition, both the db.collection.update()
method and the db.collection.save() method
can also add new documents through an
operation called an upsert.
An upsert is an operation that performs either
an update of an existing document or an insert
of a new document if the document to modify
does not exist.
INSERT A DOCUMENT WITH UPDATE()
METHOD
The following example creates a new document if
no document in the inventory collection contains
{ type: "books", item : "journal" }:
db.inventory.update( { type: "book", item :
"journal" }, { $set : { qty: 10 } }, { upsert : true } )
INSERT A DOCUMENT WITH SAVE()
METHOD
db.inventory.save( { type: "book", item:
"notebook", qty: 40 } )
MongoDB adds the _id field and assigns as its
value a unique ObjectId.
{ "_id" :
ObjectId("51e866e48737f72b32ae4fbc"),
"type" : "book", "item" : "notebook", "qty" :
40 }
4. QUERY DOCUMENTS
In MongoDB, the db.collection.find() method
retrieves documents from a collection.
The db.collection.find() method returns a cursor
to the retrieved documents.
The db.collection.findOne() method also performs
a read operation to return a single document.
Internally, the db.collection.findOne() method is
the db.collection.find() method with a limit of 1
READ OPERATIONS
Read operations, or queries retrieve data stored in
the database.
For query operations, MongoDB provide a
db.collection.find() method.
method accepts both the query criteria and
projections and returns a cursor.
SELECT ALL DOCUMENTS IN A
COLLECTION
An empty query document ({ }) selects all
documents in the collection:
db.inventory.find( { } )
OR
db.inventory.find()
Conditional Operators
SPECIFY EQUALITY CONDITION
The following example retrieves from the inventory
collection all documents where the type field has
the value snacks:
db.inventory.find( { type: "snacks" } )
Specify Conditions Using Query Operators
The following example selects all documents in the
inventory collection where the value of the type
field is either 'food' or 'snacks':
db.inventory.find( { type: { $in: [ 'food',
'snacks' ] } } )
SPECIFY AND CONDITIONS
In the following example, the query document
specifies an equality match on the field type and
a less than ($lt) comparison match on the field
price:
db.inventory.find( { type: 'food', price: { $lt: 9.95
}})
Specify OR Conditions
In the following example, the compound query
document selects all documents in the collection
where the value of the type field is 'food' and
either the qty has a value greater than ($gt) 100
or the value of the price field is less than ($lt)
9.95:
ARRAYS
{ _id: 5, type: "food", item: "aaa", ratings: [ 5, 8,
9 ]}
{ _id: 6, type: "food", item: "bbb", ratings: [ 5,
9 ]}
The following example queries for all documents
where the field ratings is an array that holds
exactly three elements, 5, 8, and 9, in this order:
db.inventory.find( { ratings: [ 5, 8, 9 ] } )
The operation returns the following document:
{ "_id" : 5, "type" : "food", "item" : "aaa",
"ratings" : [ 5, 8, 9 ] }
Example
db.users.find( { age: { $gt: 18 } }, { name: 1, address:
1 } ).limit(5)
This query selects the documents in the users
collection that match the condition age is greater
than 18.
To specify the greater than condition, query
criteria uses the greater than (i.e. $gt) query
selection operator.
The query returns at most 5 matching documents
(or more precisely, a cursor to those documents).
The matching documents will return with only the
Querying Arrays
Querying for elements of an array
is simple. An array can mostly be
treated as though each element is
the value of the overall key. For
example, if the array is a list of
fruits, like this:
> db.food.insert({"fruit" : ["apple",
"banana", "peach"]})
the following query:
> db.food.find({"fruit" : "banana"})
will successfully match the
document.
$all
Ifyou need to match arrays by more than one element, you can
use "$all
> db.food.insert({"_id" : 1, "fruit" : ["apple", "banana", "peach"]})
> db.food.insert({"_id" : 2, "fruit" : ["apple", "kumquat",
"orange"]})
db.food.insert({"_id" : 3, "fruit" : ["cherry", "banana", "apple"]})
Then we can find all documents with both "apple" and "banana"
elements by querying with "$all":
> db.food.find({fruit : {$all : ["apple", "banana"]}})
Output:-
{"_id" : 1, "fruit" : ["apple", "banana", "peach"]}
{"_id" : 3, "fruit" : ["cherry", "banana", "apple"]}
$size
A useful conditional for querying arrays
is "$size", which allows you to query for
arrays of a given size. Here’s an
example:
> db.food.find({"fruit" : {"$size" : 3}})
$slice
The special "$slice" operator
can be used to return a subset of
elements for an array key.
For example, suppose we had a
blog post document and we
wanted to return the first 10
comments:
> db.blog.posts.findOne(criteria,
{"comments" : {"$slice" : 10}})
Alternatively, if we wanted the last 10
comments, we could use -10:
db.blog.posts.findOne(criteria,
{"comments" : {"$slice" : -10}})
"$slice" can also return pages in the
middle of the results by taking an offset
and the number of elements to return:
> db.blog.posts.findOne(criteria,
{"comments" : {"$slice" : [23, 10]}})
This would skip the first 23 elements and
return the 24th through 34th. If there are
fewer than 34 elements in the array, it will
return as many as possible.