SlideShare a Scribd company logo
MongoDB

 Part I
What is "NoSQL"?

Complement, not replace, traditional relational databases
Different ways to store large quantities of denormalized
data
   Key/Value = Redis, Riak
   Tabular / "Big Table" = HBase, Cassandra
   Document = MongoDB, CouchDB
No schema
Map/Reduce
Easy scalability
RDMS is just fine

YES!
When you must guarantee transactional/ACID behavior
When you must have strict constraints
But...not when these are more important
   Faster queries
   Horizontal scalability
   Flexible design
Data Modeling with SQL
Joins? We ain't got no stinking joins
Data Modeling with MongoDB
 Beware the N+1 problem does lurk

      Having no method for joining
      means we could have a lot of
      queries on our hands if we do
      establish a large one-to-many
      relationship outside of our
      "User" document
MongoDB, BSON, and You

MongoDB does "document" storage
JSON is a popular lightweight data structure
BSON is a binary encoded serialized JSON document
Programming languages map to-from BSON easily
   Most languages have a dictionary or hash
What does it look like?
{ "_id" : ObjectId("4d240d296266ea4ff500000a"),
"first_name" : "Nick", "last_name" : "Zalabak",  
"addresses" : [ 
    {"street" : "123 Foo Bar Ave","city": "Chicago","state":
"IL"},
    {"street": "1 Mongo Blvd", "city": "Blah", "state": "CA"} ],
"social_networks" : [
     {"user_id": "user1", "sn": "twitter"}, 
     {"user_id": "user2", "sn": "facebook"}
],
"phone_numbers" : [ "123-345-1345", "555-555-5785"] }
How do I install MongoDB?

Go to http://www.mongodb.org/downloads
For Macs I'd recommend using Brew
   brew install mongodb
For Linux, it depends on your distro
   Pre-Packaged for Ubuntu/Debian
   Pre-Packaged for CentOS/Fedora/Redhat
   Good old source!
There are even installers for WinDoze
The './mongo' Console

MongoDB has a console very much like MySQL
  >./mongo
  > help
OK, how do I query to find my stuff?
The new query language you already know

                MongoDB
                <3
               JSON
              <3
          JavaScript
SQL to Mongo Operations
SQL Statement                                    Mongo Query Language Statement



CREATE TABLE USERS (a Number, b Number) implicit. MongoDB is "lazy".
INSERT INTO USERS VALUES(1,1)           db.users.insert({a:1,b:1})db.users.find({}, {a:1,b:1})
SELECT a,b FROM users



SELECT a,b FROM users WHERE                      db.users.find({age:33}, {a:1,b:1})db.users.find({'age':
age=33SELECT * FROM users WHERE                  {$gt:33}})})db.users.find({name:/Joe/})
age>33SELECT * FROM users WHERE name
LIKE "%Joe%"

EXPLAIN SELECT * FROM users WHERE z=3SELECT      db.users.find({z:3}).explain()db.users.distinct('last_name') db.
DISTINCT last_name FROM users UPDATE users SET   users.update({b:'q'}, {$set:{a:1}}, false, true)
a=1 WHERE b='q'




DELETE FROM users WHERE z="abc"                  db.users.remove({z:'abc'});
The Compelling Sales Pitch

More Related Content

MongoDB

  • 2. What is "NoSQL"? Complement, not replace, traditional relational databases Different ways to store large quantities of denormalized data Key/Value = Redis, Riak Tabular / "Big Table" = HBase, Cassandra Document = MongoDB, CouchDB No schema Map/Reduce Easy scalability
  • 3. RDMS is just fine YES! When you must guarantee transactional/ACID behavior When you must have strict constraints But...not when these are more important Faster queries Horizontal scalability Flexible design
  • 5. Joins? We ain't got no stinking joins
  • 7.  Beware the N+1 problem does lurk Having no method for joining means we could have a lot of queries on our hands if we do establish a large one-to-many relationship outside of our "User" document
  • 8. MongoDB, BSON, and You MongoDB does "document" storage JSON is a popular lightweight data structure BSON is a binary encoded serialized JSON document Programming languages map to-from BSON easily Most languages have a dictionary or hash
  • 9. What does it look like? { "_id" : ObjectId("4d240d296266ea4ff500000a"), "first_name" : "Nick", "last_name" : "Zalabak",   "addresses" : [      {"street" : "123 Foo Bar Ave","city": "Chicago","state": "IL"},     {"street": "1 Mongo Blvd", "city": "Blah", "state": "CA"} ], "social_networks" : [      {"user_id": "user1", "sn": "twitter"},       {"user_id": "user2", "sn": "facebook"} ], "phone_numbers" : [ "123-345-1345", "555-555-5785"] }
  • 10. How do I install MongoDB? Go to http://www.mongodb.org/downloads For Macs I'd recommend using Brew brew install mongodb For Linux, it depends on your distro Pre-Packaged for Ubuntu/Debian Pre-Packaged for CentOS/Fedora/Redhat Good old source! There are even installers for WinDoze
  • 11. The './mongo' Console MongoDB has a console very much like MySQL >./mongo > help OK, how do I query to find my stuff?
  • 12. The new query language you already know  MongoDB <3 JSON <3 JavaScript
  • 13. SQL to Mongo Operations SQL Statement Mongo Query Language Statement CREATE TABLE USERS (a Number, b Number) implicit. MongoDB is "lazy". INSERT INTO USERS VALUES(1,1) db.users.insert({a:1,b:1})db.users.find({}, {a:1,b:1}) SELECT a,b FROM users SELECT a,b FROM users WHERE db.users.find({age:33}, {a:1,b:1})db.users.find({'age': age=33SELECT * FROM users WHERE {$gt:33}})})db.users.find({name:/Joe/}) age>33SELECT * FROM users WHERE name LIKE "%Joe%" EXPLAIN SELECT * FROM users WHERE z=3SELECT db.users.find({z:3}).explain()db.users.distinct('last_name') db. DISTINCT last_name FROM users UPDATE users SET users.update({b:'q'}, {$set:{a:1}}, false, true) a=1 WHERE b='q' DELETE FROM users WHERE z="abc" db.users.remove({z:'abc'});