2. 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.
3.
4. 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
5. 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"]
}
6. 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)
7. Inserts – Easy As Pie!
use cookbook;
db.desserts.save({
name: "Cherry Pie",
ingredients: ["cherries", "pie"],
cooking_time: 30
});
8. Searching – A Piece Of Cake
db.recipes.find({
cooking_time: { “$gte”: 10, “$lt”: 30}
})
db.recipes.findOne()
9. 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)) });
10. 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.
11. 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});
}
12. 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;}
}
13. Replica Sets
Master/Slave configuration
If your primary server goes down, one of
the secondary ones takes over.