Press:
- Space or ← / → to move around
- Ctrl/Command / – or + to zoom in and out if slides don’t fit
- N to show/hide speaker notes
- H to highlight important elements in code snippets
Credits:
These slides are based on HTML5Wow from Google
The Good, The Bad and the Ugly
crodas@php.net
11:15 - 12:00pm
Talk about MongoDB
about how I screwed up in the past (and what I've learned)
At the end of this talk, ask me in English, Spanish or Portuguese.
This Slides are open source http://github.com/crodas/Slides
- From Paraguay
- Uses PHP and NodeJS
- Early adopter of Mongo
- Loves Open Source
a database (like MySQL)
without SQL
friendly as Doctrine (but without overhead)
MyDoctrineWithoutOverhead
bad joke I know
db.users.save({
_id: 4290392,
name: "César Rodas",
emails: [
{email: "saddor@gmail.com", jabber:true, mail:true},
{email: "crodas@php.net", mail:true, jabber: false},
{email: "jabber@crodas.org", mail:false, jabber: true},
],
likes: ["PHP", "NodeJS", "MongoDB"],
age: 23,
address: {
country: "Paraguay",
city: "Asunción",
}
});
- Semi-structured model
- Array (PHP)
- Dictionary (Python)
- Hash (Perl)
- Object (without methods)
- Everything is a document
- Fast at query time.
- Simplest data distribution.
- Less painful migrations.
- UTF-8 friendly.
- Type-rich supports dates, regular expressions, code, binary data, and more.
var cursor = db.users.find({"age": 4290392});
cursor = db.users.find({"likes": "PHP", "age":23});
cursor = db.users.find({"address.country": "Paraguay"});
cursor = db.users.find({"age": {"$gt": 18}});
cursor = db.users.find({"emails.mail": true});
cursor = db.users.find({"emails": {"$elemMatch":{"mail": false, "jabber":true}}});
- Indexes are optional.
- Speed up queries, slow inserts (updates?).
- Could be single-key, compound, unique, non-unique, and geospatial.
- The query optimizer will try a number of different query plans when a query is run and select the fastest, periodically resampling.
db.users.find({sex:"male"});
// Takes 341ms
db.users.find({sex:"male"}).explain()
{
"cursor" : "BasicCursor",
"nscanned" : 11732,
"nscannedObjects" : 11732,
"n" : 843,
"millis" : 36,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
}
}
db.users.ensureIndex({sex:"male"})
db.users.find({sex:"male"}).sort({age:-1}).explain()
{
"cursor" : "BtreeCursor sex",
"nscanned" : 843,
"nscannedObjects" : 843,
"n" : 843,
"scanAndOrder" : true,
"millis" : 27,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"sex" : [
...
]
}
}
db.users.ensureIndex({sex:"male", age:-1})
db.users.find({sex:"male"}).sort({age:-1}).explain()
{
"cursor" : "BtreeCursor sex__age_-1",
"nscanned" : 843,
"nscannedObjects" : 843,
"n" : 843,
"millis" : 2,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"sex" : [
...
],
"age" : [
...
]
}
}
db.foo.ensureIndex({"address.country":1});
address: {
country: "Brazil",
city: "Sao Paulo",
}
address: [
{
country: "Brazil",
city: "Sao Paulo",
},
{
country: "Asunción",
city: "Paraguay",
},
]
db.foo.ensureIndex({"address.country":1});
db.foo.ensureIndex({"address":1});
- Measures slow operations
- It is saved at
system.profilecollection - Measure query, update, insert and getmore
- Similar to explain()
- http://www.mongodb.org/display/DOCS/Database+Profiler
Designing documents
- Data redundancy
- Embedding vs Referencing