18
Introduction to Athens MongoDB User group hanasis Efthymiou ervice Delivery Manager

Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Embed Size (px)

DESCRIPTION

##### 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.

Citation preview

Page 1: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Introduction to

Athens MongoDB User group

Thanasis EfthymiouService Delivery Manager

Page 2: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

2

But wait!

What’s our group all about?

Page 3: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

3

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

Page 4: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

4

Our Sponsors

Page 5: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

5

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

Page 6: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

6

Replica set sneak peek

Primary

Page 7: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

7

Sharding sneak peek

Page 8: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

8

So what is ?

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 }

SQL world MongoDB

Database Database

Table Collection

Index Index

Row Document

Column Field

Joining Embedding & linking

Page 9: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

9

So what is ?• Map/Reduce

– Map: transform and filter data – 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

SELECT and WHERE clause

GROUP BY clause

Page 10: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

10

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…

vroom, vroom!

Page 11: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

11

Geospatial index exampleCreate it: db.places.ensureIndex( {coord : '2d'} )Use it: db.places.find( { coord: {$near : [38.008003, 23.865303] } } )

Page 12: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

12

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

Page 13: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

13

Querying>db.people.insert( {name:"Thanasis",'company':"Persado","address":"Kastorias 4, 15344, Gerakas",likes:["photography", "cycling"],drinksCoffee:true} )> >db.people.find(){ "_id" : ObjectId("50f57146fcb8b36343367b8f"), "name" : "Thanasis", "company" : "Persado", "address" : "Kastorias 4, 15344, Gerakas", "likes" : [ "photography", "cycling" ], "drinksCoffee" : true }>db.people.find().pretty(){ "_id" : ObjectId("50f57146fcb8b36343367b8f"), "name" : "Thanasis", "company" : "Persado", "address" : "Kastorias 4, 15344, Gerakas", "likes" : [ "photography", "cycling" ], "drinksCoffee" : true}

Page 14: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

14

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: {$elemMatch: {b: 1, c: 2}} }a is an array containing a single item with both b equal to 1 and c equal to 2

{ $or: [{a: 1}, {b: 2}] } a is 1 or b is 2

{ a: /^m/ } a begins with the letter “m”

Page 15: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

15

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

Page 16: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

16

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 = 'Bob'

db.users.update({name: "Bob"}, {$set: {age: 33}}, false, true)

DELETE FROM users WHERE name = 'Bob' db.users.remove({name: "Bob"})

Page 17: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

17

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

Page 18: Intro to MongoDB by Thanasis Efthymiou, Athens MongoDB User Group

Thanasis Efthymiou,

18

http://www.meetup.com/Athens-MongoDB/Don’t forget to RSVP Yes to our Events!

[email protected]

www.facebook.com/MongoDBGreece

www.linkedin.com/groups/MongoDB-Greece-4731560

Thank you!