Introduction tomongodb

16
AN INTRODUCTION TO

description

 

Transcript of Introduction tomongodb

Page 1: Introduction tomongodb

AN INTRODUCTION TO

Page 2: Introduction tomongodb

But First, A Little About Me My name’s Lee Theobald. Hello! Twitter: @Leesy Never updated blog:

http://www.ltheobald.co.uk/ I’m a developer at a small search

company in Cambridge called Open Objects.

Page 3: Introduction tomongodb

SO WHAT IS MONGODB?

Page 4: Introduction tomongodb

NoSQL database The name is short for humongous Open source with development lead

by 10Gen (http://www.10gen.com) Document Based Schema-less Highly Scalable MapReduce Replication & Auto Sharding

Page 5: Introduction tomongodb

Document Store Data is stored in the form of JSON data.

{ "_id" : ObjectId("4ccbfc75bd163019417c27f8"), "title": “Hello World! ", "author": { "firstname": "Joe", "lastname": "Bloggs" }, "tags": ["test", "foo", "bar"] }

Page 6: Introduction tomongodb

Familiar Structure

MongoDB instances contain a number of databases which in turn contain a number of collections.

Think of MySQL with it’s databases & tables.

But collections can also be nested. E.g.customer_a (Database)○ logs_201009 (Collection)

top_queries(Collection)

Page 7: Introduction tomongodb

Inserts – Easy As Pie!

use cookbook;db.desserts.save({ name: "Cherry Pie", ingredients: ["cherries", "pie"], cooking_time: 30});

Page 8: Introduction tomongodb

Searching – A Piece Of Cake

db.recipes.find({ cooking_time: { “$gte”: 10, “$lt”: 30}})

db.recipes.findOne()

Page 9: Introduction tomongodb

Some More Advanced Query Syntax Limiting

db.find().limit(10); Skipping

db.find( ).skip(5); Sorting

db.find({…}).sort({cooking_time: “-1”}); Cursors

var cur = db.find({…}).cursor()cur.forEach( function(x)

{ print(tojson(x)) });

Page 10: Introduction tomongodb

MapReduce

Great way of doing bulk manipulation or aggregation.

2 or 3 functions written in JavaScript that execute on the server.

An example use could be generating a list of top queries from some search logs.

Page 11: Introduction tomongodb

Map Function Takes some input of the form of key/value

pairs. Does something with that Returns 0 or more key/value pairs

map = function() { if (!this.query) { return; } emit (this.query, {count: 1});}

Page 12: Introduction tomongodb

Reduce Function Takes the results from the map function Does something (normally combine the results) Produces output in key/value pairs

reduce = function(key, values) { var count = 0; values.forEach(function(v) { count += v[‘count’]; } return {count: count;}}

Page 13: Introduction tomongodb

Replica Sets

Master/Slave configuration If your primary server goes down,

one of the secondary ones takes over.

Page 14: Introduction tomongodb

Auto Sharding

Page 15: Introduction tomongodb

But Why Not Try It Yourself Download it at:

http://www.mongodb.org Online tutorial at:http

://www.mongodb.org/display/DOCS/Tutorial

Some handy MongoDB sites:MongoDB Cookbook:

http://cookbook.mongodb.org/Kyle Banker’s blog:

http://kylebanker.com/blog/

Page 16: Introduction tomongodb

Thanks For Listening.Any (easy) Questions?