##### Note: This is a non-static Powerpoint presentation, so it's recommended to download it and view it locally rather than online. ##### Introduction to MongoDB presented to our Athens MongoDB user group on 16th of January, 2013: an overview of what it is, some of the features, a few queries and things yet to be explored.
3. Thanasis Efthymiou,
What we want is to:
• Explore MongoDB and related technologies
– “NoSQL” design culture
– Map/Reduce
– Hadoop, Hive, …
• Learn from each other while having fun
• Connect with the experts (and become one)
• Free coffee
3
5. Thanasis Efthymiou,
So what is ?
• Open-source database
– Free to download and use
– Subscription services for support offered by 10gen
• Document-oriented database
– Fields, documents and collections instead of columns, rows and tables as in RDBMS
– Flexible and dynamic schema, no predefined or rigid as in RDBMS
– JSON documents, e.g.: {field1 : value1, field2 : value2 }
– Possibility to embed documents within others, e.g.: {f1 : v1, f2 : { f3: v3, f4: v4 } }
• Built for scalability and high availability
– Replica sets with automatic failover for redundancy
– Auto-sharding for performance and load balancing
– Built for the web, built to live in the fluffy clouds
• Rich query interface
– Javascript-powered shell
• Easy to setup and administer
5
8. Thanasis Efthymiou,
So what is ?
SQL world MongoDB
Database Database
Table Collection
Index Index
Row Document
Column Field
Joining Embedding & linking
JSON document examples
• {a:1}
• { b : 2, c : "Hello world!", d : new Date('Jan 16, 2013')}
• { name : "Thanasis",
'company' : "Persado",
"address" : "Kastorias 4, 15344, Gerakas",
likes : ["photography", "cycling"],
drinksCoffee : true }
8
9. Thanasis Efthymiou,
So what is ?
GROUP BY
• Map/Reduce SELECT and
clause
– Map: transform and filter data WHERE clause
– Reduce: combine multiple rows into fewer records, i.e. aggregate
• Hadoop connector
– If you have really, really huge data sets
• Capped collections
– Guarantee preservation of the insertion order
– Guarantee insertion order is identical to the order on disk
– Powerful for logging
• GridFS
– Storing/retrieving files exceeding the 16MB document size limit (e.g. video)
– Divides a file into parts, or chunks and stores them as separate documents
9
10. Thanasis Efthymiou,
So what is ?
• It goes…
– Said to be 2 to 10 times faster than MySQL, depending on the “write concern”
• Fire and forget versus wait for acknowledgement from n servers
– Designed to be shared across multiple machines
– In-place updates (typical example the incremental operator $inc)
– Typically makes use of commodity servers with enough RAM to keep the entire data set
in memory
• Great index support
– Pretty similar concepts apply as in RDBMS
– Take it easy on the indexes, your write operations might slow down
– The query optimizer selects the index empirically by occasionally running alternate
query plans and by selecting the plan with the best response time
– Can be unique, compound, multikey indexes on arrays, sparse
– Can be created in a non-blocking fashion in the background
– And the mobile web’s favorite…
10
11. Thanasis Efthymiou,
Geospatial index example
Create it: db.places.ensureIndex( {coord : '2d'} )
Use it: db.places.find( { coord: {$near : [38.008003, 23.865303] } } )
11
12. Thanasis Efthymiou,
is not…
• SQL RDBMS like Oracle, MySQL or SQL Server
– No SQL supported but does have rich query syntax
– No transactions (and no commit necessary!)
– No referential integrity of data, no foreign keys, no constraints, no triggers!
– No joins!
• What? No joins??
– Go for embedded documents or
– Emulate joining in your application (multiple queries using code)
• No stored procedures or views, but stored Javascript
• No tabular interface / data grid looks
– No Toad or SQL Developer clients
– No easy data copy-paste to Excel
• No Alter Table commands that can take minutes or hours
12
14. Thanasis Efthymiou,
Querying
>db.people.find( … )
{ a: 10 } a is 10, or an array containing the value 10
{ a: 10, b: “hello” } a is 10 and b is “hello”
{ a: {$gt: 10} } a is greater than 10. Also $lt (<), $gte (>=), $lte (<=), and $ne (!=)
{ a: {$in: [10, “hello”]} } a is either 10 or “hello”
{ a: {$all: [10, “hello”]} } a is an array containing both 10 and “hello”
{ “a.b”: 10 } a is an embedded document with b equal to 10
a is an array containing a single item with both b equal to 1 and c equal
{ a: {$elemMatch: {b: 1, c: 2}} } to 2
{ $or: [{a: 1}, {b: 2}] } a is 1 or b is 2
{ a: /^m/ } a begins with the letter “m”
14
15. Thanasis Efthymiou,
Updating
>db.people.update( query, update, <upsert>, <multi> )
>db.people.update({company:"Upstream"}, {$set:{company:"Persado"}},
false, true)
>db.people.update({company:"Upstream"}, {company:"Persado"})
{ $inc: {a: 2} } Increment a by 2
{ $set: {a: 5} } Set a to the value 5
{ $unset: {a: 1} } Delete the a key
{ $push: {a: 1} } Append the value 1 to the array a
{ $pushAll: {a: [1, 2]} } Append both 1 and 2 to the array a
{ $addToSet: {a: 1} } Append the value 1 to the array a (if it doesn’t already exist)
{ $addToSet: {a: {$each: [1, 2]}} } Append both 1 and 2 to the array a (if they don’t already exist)
{ $pop: {a: 1} } Remove the last element from the array a
{ $pop: {a: -1} } Remove the first element from the array a
{ $pull: {a: 5} } Remove all occurrences of 5 from the array a
{ $pullAll: {a: [5, 6]} } Remove all occurrences of 5 or 6 from the array a
15
16. Thanasis Efthymiou,
SQL versus
SQL MongoDB
SELECT * FROM users db.users.find()
INSERT INTO users VALUES ('Bob', 32) db.users.insert({name: "Bob", age: 32})
SELECT name, age FROM users
WHERE age = 33 db.users.find({age: 33}, {name: 1, age: 1, _id:0})
SELECT * FROM users WHERE age = 33
ORDER BY name ASC db.users.find({age: 33}).sort({name: 1})
SELECT * FROM users WHERE age > 33 db.users.find({age: {$gt: 33}})
SELECT * FROM users
WHERE name LIKE '%Joe%' db.users.find({name: /Joe/})
SELECT COUNT(*) FROM users
WHERE AGE > 30 db.users.find({age: {$gt: 30}}).count()
UPDATE users SET age = 33 WHERE name = db.users.update({name: "Bob"}, {$set: {age: 33}}, false,
'Bob' true)
DELETE FROM users WHERE name = 'Bob' db.users.remove({name: "Bob"})
16
17. Thanasis Efthymiou,
Find out more!
Download and documentation http://www.mongodb.org/
Use cases, training, everything http://www.10gen.com/
Online training portal – don’t miss! https://education.10gen.com/
API and drivers http://api.mongodb.org/
If you’re still hard to satisfy http://www.google.com
17
18. Thanasis Efthymiou,
Thank you!
http://www.meetup.com/Athens-MongoDB/
Don’t forget to RSVP Yes to our Events!
thanasis.efthymiou@gmail.com
www.facebook.com/MongoDBGreece
www.linkedin.com/groups/MongoDB-Greece-4731560
18