latinoware
Post on 25-May-2015
953 Views
Preview:
TRANSCRIPT
base de dados orientada a
documentos para aplicações Web
Kristina Chodorow
Who am I?
Software Engineer at
Here from New York City
Why Use
Non-Relational DBs?
CAP
• Consistency
• Availability
• Partitioning
nome : "joe" nome : "joe"
Key-Value Stores
Key Value
foo bar
x 123
ts 6:13:26 12/3/1945
data \x9F\x44\x1e
Tokyo Cabinet, Dynamo, MemcacheDB
Key-Value Stores
•Fast
•Simple•Too simple
Column-Oriented
Cassandra
Document-Oriented
{
date : Date(2007-05-07),
time : {
start : 9.25,
end : 10.25
}
sum : 0,
comments : ["task 1", "run 4", "testing module"]
}
vs.
Consistency
Availability
Partitioning
master
slave
Consistency
Availability
Partitioning
slave
Consistency
Availability
Partitioning
master
Consistency
Availability
Partitioning
master
slave
Consistency
Availability
Partitioning
Router
Pair
Pair
Pair
Consistency
Availability
Partitioning
...eventual consistency
Introduction to MongoDB
A JavaScript Database
$ mongodb-linux-1.0/bin/mongo
MongoDB shell version: 1.0.0
url: test
connecting to: test
type "help" for help
>
JSON and BSONStrict JSON types:
{
x : null, y : true, z : 123, w : "xyz",
a : { b : 1 }, c : [1, 2, 3]
}
Mongo JSON adds:
{
ts : new Date(),
query : /regex/ig,
_id : new ObjectId()
}
Collections, not Tables
Collections
{
date : Date(2007-05-07),
time : {
start : 9.25,
end : 10.25
}
sum : 0,
comments : ["task 1", "run 4"]
}
Using the Shell
> db
test
> use xyz
> db
xyz
> db.splorch.find()
>
Inserting
> db.foo.insert({name : "Joe", age : 34})
> db.foo.find({name : "Joe"})
{
"_id" :
ObjectId("2fe3e4d892aa73234c910bed"),
"name" : "Joe",
"age" : 34
}
Object Idsan autogenerated primary key
"_id" : ObjectId("2fe3e4d892aa73234c910bed")
12 bytes:
2fe3e4d892aa73234c910bed
|------||----||--||----|
ts mac pid inc
Nested Objects
> db.blog.insert({title : "First Post",
content : "Hello, world!",
author : {name : "Joe", id : 123},
comments : []
})
Querying
posts = db.blog.find({
"author" : "Joe"})
commentsByFred = db.blog.find({
"comments.author" : "Fred"})
commentedByFred = db.blog.find({
"comments.author" : /fred/i})
Speaking of indexing…
db.people.ensureIndex({"age" : 1});
db.people.ensureIndex({
"name" : -1,
"ts" : -1,
"comments.author" : 1
});
Updating
db.blog.update({title : "First Post"}, {
$push : {
comments : {
author : "Fred",
comment : "Dumb post."
}
}
});
…which gives us:
> db.blog.findOne()
{
_id : ObjectId("4ae06192213900000000745c"),
"title" : "First Post",
"content" : "Hello, world!"
"author" : {"name" : "Joe", "id" : 123}
"comments" : [{
"author" : "Fred",
"comment" : "Dumb post"
}]
}
$ instead of >, <, =, etc.
$gt, $gte, $lt, $lte, $eq, $neq, $exists,
$set, $mod, $where, $in, $nin, $inc
$push, $pull, $pop, $pushAll, $popAll
db.foo.bar.find({x : {$gt : 4}})
$where
db.blog.findOne({$where :
'this.y == (this.x + this.z)'});
Will work:
{"x" : 1, "y" : 4, "z" : 3}
{"x" : "hi", "y" : "hibye", "z" : "bye"}
Won’t work:
{"x" : 1, "y" : 1}
Optimizing $where
db.blogs.findOne({
name : "Sally",
age : {'$gt' : 18},
$where : 'Array.sort(this.interests)[2]
== "volleyball"'});
Cursors
cursor = db.blah.find(array("foo" : "bar"))
while (cursor.hasNext()) {
obj = cursor.next()
}
Applications
Dextify2
FetLife
soliMAP
@trackmeet
Paging
cursor = db.results.find()
.sort({"ts" : -1})
.skip(page_num * results_per_page)
.limit(results_per_page);
Logging
• insert/update/remove is fast
• Capped collections
• Schemaless
• $inc for counts
Storing Files
Max: 4Mb
Storing Files
(More than 4 Mb)
Storing Files
chunks
JJ J
J
JJ
J
J
J
J_id :files
Storing Files
ObjectId fileId = new ObjectId();
fileObj = {
_id : fileId,
filename : "ggbridge.png",
user : "joe",
takenIn : "San Francisco"
}
chunkObj = {
fileId : fileId,
chunkNum : N
data : <binary data>
}
Aggregation
group =~ GROUP BY
Map/Reducedb.runCommand({
mapreduce : <collection>,
map : <mapfunction>,
reduce : <reducefunction>
[, query : <query filter object>]
[, out : <outputcollectionname>]
[, keeptemp: <true|false>]
[, finalize : <finalizefunction>]
})
www.mongodb.org
Drivers
C#, Erlang, Factor
Thank you!
kristina@10gen.com
@kchodorow
@mongodb
irc.freenode.net#mongodb
www.mongodb.org
top related