2. I will talk ….
•What is MongoDB ?
•Queries in MongoDB
•Replication
•Sharding
•Other Cool Features
•Code samples
•Performance
• When to Use & not Use?
3. What is MongoDB?
•Document-oriented
•JSON-style documents (Actually BSON)
•Dynamic Schema
•Non Relational (NOSQL)
•Performance
•Written in C++ for high
•Full Index support
•No Transactions (but supports atomic operations)
•Not relational
•Scalability
•Replication
•Shardinges
4. Who is using MongoDB?
See more @ http://www.mongodb.org/display/DOCS/Production+Deployments
5. Document Oriented??
You can store and access data in real life document style.
- Can have any number of fields.
- Can contain other documents(comments in below example)
- Can store array/collection elements (e.g tags)
- Not restricted to strict columns and rows. Document can con
For RDBMS people, its denormalized view by combining multiple tables relational
databases.
{
“title”: “NoSQL Database Internals”,
“date”: “Tue Oct 27 2009 17:24:18 GMT-0400 (EDT)”,
“content” : “ Database internals …….”,
“author” : “xxxxxx” ,
“tags”: [“nosql”, “databases”, “c++”],
“comments”: [
{“author”: “Fred”,
“text”: “Quite”
}
]
}
6. How to access/query data?
db.post.find() ➔ SELECT * FROM post
db.posts.find({“author”:“Kyle”})
db.posts.find(“tags”: “nosql”)
db.posts.find({“date”: {“$lte”: Date.now})
db.post.find({“comments.username”: “eh”})
7. Insert/Update/Alter?
Insert
db.user.insert({‘name’ : ‘foobar’, ‘age’ : 25}) ➔
INSERT INTO user (`name`, `age`) values ('foobar',25)
Update
db.user.update({'name' : 'foobar'}, {$set : {'age' : 36}}) ➔
UPDATE user SET `age` = 36 WHERE `name` = 'foobar‘
if you want add a column `email`
db.user.insert({‘name’ : ‘foobar’, ‘age’ : 25, ‘email’ : ‘foo@bar.com’}) ➔
ALTER TABLE user….
Delete
db.user.remove({'age' : {$lt : 30}}) ➔ DELETE FROM user WHERE age <
30
8. But why should i use instead of
RDBS(say mysql)?
● If you got huge data, thats difficult to manage in RDBMS, without lot of
firefight.
● Dynamic schema, application can add new fields just by adding fields
in Object mapping collection(table).
● Faster development, as you don’t need alter table scripts and don’t
need to split your data into columns.
● Easier to scale and shard.
● New shiny thing?
9. DriversDrivers / Libraries
•All most all Famous languages have drivers for mongodb. More details @
http://www.mongodb.org/display/DOCS/Drivers
•OR M (Object relational mappers)
o Java - morphia, spring data
o Ruby - mongoid, mongoMapper
o Php - doctrine, mongodloid
o node.js - Mongoose.
12. Other Cool Features
• Grid FS
• Store files of any size
• GridFS will leverage any existing replication or autosharding that you’ve set up for
MongoDB, so getting failover and scale-out for file
•Map Reduce
•Queries run in all shards parallel
•Flexible aggregation and data processing
•Geospatial Indexing
•Find objects based on location – Find closest n items to x
e.g
db.map.insert({location : {longitude : -40, latitude : 78}})
db.map.find({location : {$near : [ -30, 70]})
15. Code Samples : Music Band
Band
Contact Info
Distributor
Song
1
10….
*
0….
*
16. When Not to use it / Drawbacks?
• If your data is not huge and comfortably fits in RDBMS
• If you need transactional features.
• If you have complex Relationships.
• Learning curve, querying etc is not hard in mongo but
almost everyone knows SQL syntax.
• Global write lock.
• Duplicate data and data integrity as same piece of data
may be embedded in multiple documents.
• You have to manage any relationships at application layer.
17. Nuggets
• Consider it only when your data is huge or doesn’t really
suit relational structure.
• Very easy & enjoyable at development phase but as
application grows data stored might get messy. So store
and retrieve data using ORM objects only and be careful
while changing object fields e.g renaming.
• Relationships ,constraints, integrity etc have to be
managed at application level, unlike RDBMS where all
these are built in.
• Don’t stuff everything in single document, use references to
other documents when appropriate.
• Querying is not hard but takes some time to get used to it,
so not easy as querying in SQL.