Introduction to
MongoDB
Get started on your way to being a NoSQL Ninja!
About Me
technology
BUSINESS
About Me
About Me
Points of Discussion
What is MongoDB?
SQL vs NoSQL
Document Data Model
CRUD operations
Cursor concepts
Benefits & drawbacks
Use cases & “Don’t Use!” cases
MONGODB
What is MongoDB?
Open-source
Document-oriented
NoSQL database
But what is NoSQL?
- Alternative mechanism for data storage
- Addresses challenges faced by SQL databases
DOCUMENT
Basic unit of data in MongoDB
Analogous to row in relational database
Obtained as JSON object
Exists on disk as BSON
COLLECTION
Group of documents
Analogous to table in relational database
Does not enforce a schema
Documents in a collection usually have similar purpose
DATABASE
Container for collections
Stores files on disk
A single MongoDB server usually has multiple databases
Collection 1
Collection 3
Collection 2
Collection 4
SQL vs NoSQL
Differences between SQL & NoSQL
SQL NoSQL
Table-based Document or key-value pair based
Predefined schema Dynamic schema, or no schema
Vertically scalable Horizontally scalable
Differences between SQL & NoSQL
SQL NoSQL
Better for relational data Better for hierarchical data
Better for transactions Better for faster IO
Examples:
MySql, Oracle, MS-SQL etc
Examples:
MongoDB, CouchDB, RethinkDB etc
Relational Database
NoSQL Database
DOCUMENT DATA MODEL
Relational MongoDB
id hero game
0 Commander Shepard Mass Effect
1 Hawke Dragon Age
id name type hero_id
0 Sniper rifle Ranged combat 0
1 Daggers Melee combat 1
2 Stealth Class skill 1
3 Tactical cloak Class skill 0
HERO
SKILL
{
_id: <ObjectID>,
name: “Commander Shepard”,
game: “Mass Effect”,
skills: [{
name: “Sniper rifle”,
type: “Ranged combat”,
}, {
name: “Tactical cloak”,
type: “Class skill”,
}]
}
HERO
MongoDB Documents are Typed
{
name : “J. Shepard”,
title : “Commander”,
Address : {
address1:
“Tiberius Tower”,
address2:
“Citadel”,
pincode : “11E
SR2”,
}
expertise: [ “Sniper”, “Combat”, “Tech”, “Reapers”],
employee_number : 320,
}
CRUD OPERATIONS
Insert
db.games.insert(
{
name: “Mass Effect”,
year: “2007”,
publisher: “BioWare”
}
)
Find
db.games.find(
{ year: “2007” },
{ name: 1, publisher: 1, _id: 0 }
)
Result:
[{
name: “Mass Effect”,
publisher: “BioWare”
}]
Update
db.games.update(
{ year: “2007” },
{ $set: { publisher: “EA” } },
{ multi: true }
)
Updated Document:
{
name: “Mass Effect”,
year: “2007”
publisher: “EA”
}
Remove
db.games.remove(
{ publisher: “EA” }
)
CURSOR
Cursor
A pointer to the result set of a query
Clients can iterate through a cursor to retrieve results
The find queries we saw before return a cursor by default:
db.games.find({ year: “2007” })
a cursor
Cursor Iteration
Calling cursor.next() returns
the next document
Cursor points to each
document in the results
MongoDB result as
set of documents
Now let’s see some cursor operations.
Count
Returns the total number of results in a cursor.
Example:
db.games.find(
{ year: “2007” }
).count()
Sort
Sorts the results of a cursor based on given key and order.
Example:
db.games.find({}).sort(
{ year: -1 }
)
sort criteria
Limit
Limits the results of a cursor based on given number.
Example:
db.games.find(
{ year: “2007” }
).limit(5)
Skip
Specifies the offset for returning results.
Example:
db.games.find(
{ year: “2007” }
).skip(5)
OBSERVATION
AND
CONCLUSION
Atomicity & Transactions
MongoDB:
Supports atomicity within a single document
Does not support transactions or rollbacks
If your application requires transactions:
- Use a relational database for that part of operations
Benefits of MongoDB
Schema-less design
Cost effectiveness
Quick start & fast learning
Replica sets
Auto Sharding
Strong community
Rapidly evolving technology
Drawbacks of MongoDB
No automatic joins
No transactions
Chances of data inconsistency are slightly higher
Data size in MongoDB is typically higher
When to use MongoDB
Real time application
Need for fast I/O
Storing data with little to no structure
Fast scaling required
Rapid development
When to not use MongoDB
Data has a lot of relations
ACID compliancy is needed
(Atomicity, Consistency, Isolation, Durability)
Transactions and rollbacks are needed
Data is structured and unchanging
THANK YOU !
ANY QUESTIONS?

Introduction to MongoDB