5. Points of Discussion
What is MongoDB?
SQL vs NoSQL
Document Data Model
CRUD operations
Cursor concepts
Benefits & drawbacks
Use cases & “Don’t Use!” cases
12. 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
13. 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
25. 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
26. 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.
27. Count
Returns the total number of results in a cursor.
Example:
db.games.find(
{ year: “2007” }
).count()
28. Sort
Sorts the results of a cursor based on given key and order.
Example:
db.games.find({}).sort(
{ year: -1 }
)
sort criteria
29. Limit
Limits the results of a cursor based on given number.
Example:
db.games.find(
{ year: “2007” }
).limit(5)
32. 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
33. Benefits of MongoDB
Schema-less design
Cost effectiveness
Quick start & fast learning
Replica sets
Auto Sharding
Strong community
Rapidly evolving technology
34. Drawbacks of MongoDB
No automatic joins
No transactions
Chances of data inconsistency are slightly higher
Data size in MongoDB is typically higher
35. When to use MongoDB
Real time application
Need for fast I/O
Storing data with little to no structure
Fast scaling required
Rapid development
36. 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