Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)

Preview:

Citation preview

Kristina Chodorow

MapReduce, Geospatial Indexes, and Other Cool Features

connecting to your mongod

connecting to your mongod

you

connecting to your mongod

you

mongod

mongod

mongod

mongodupdate query insertinsert query

mongod

driver

driver

insert

query

mongod

driver

insert

query

mongod

driver

insertgetlasterror

mongod

driver

insertgetlasterror

Dwight @ 3:15Farallon Room

replication

insert this

all set

insert this

all set

make sure two slaves have this

all set

make sure two slaves have this

> db.runCommand(... getlasterror : 1... )

> db.runCommand(... getlasterror : 1,... w : 3... )

> db.runCommand(... getlasterror : 1,... w : 500... )

> db.runCommand(... getlasterror : 1,... w : 500,... wtimeout : 3000... )

> db.runCommand(... getlasterror : 1,... fsync : true... )

some internals

insertupdateremovequeryget more

Operations:

insertupdateremovequeryget more

Operations:

insertupdateremovequeryget more

Operations:

insertupdateremovequeryget more

Operations:

insertupdateremovequeryget more

Operations:

insertupdateremovequeryget more

Operations:

insertupdateremovequeryget more

Operations:

get help

$ mongoMongoDB shell version 1.5.1url: testconnecting to: testtype "help" for help>

$ mongoMongoDB shell version 1.5.1url: testconnecting to: testtype "help" for help>

> db.help()DB methods:

db.addUser()db.auth()db.cloneDatabase()db.commandHelp()db.copyDatabase()db.createCollection()db.currentOp()...

> db.foo.help()DBCollection help

db.foo.count()db.foo.dataSize()db.foo.distinct()db.foo.drop()db.foo.dropIndex()db.foo.dropIndexes()...

> var cursor = db.foo.find()> cursor.help()DBQuery help

.sort()

.limit()

.skip()

.count()

.size()

...

http://api.mongodb.org/js

> db.foo.count()40>

> db.foo.count()40> db.foo.count

> db.foo.count()40> db.foo.countfunction(x) return this.find(x).count();;

>

> db.foo.count()40> db.foo.countfunction(x)

return this.find(x).count();;> var cursor = db.foo.find()>

> db.foo.count()40> db.foo.countfunction(x)

return this.find(x).count();;> var cursor = db.foo.find()> cursor.count

> cursor.countfunction(applySkipLimit) var cmd = count : this._collection.getName();;if (this._query) if (this._special) cmd.query = this._query.query;;

else cmd.query = this._query;;

cmd.fields = this._fields || ;;if (applySkipLimit)

if (this._limit) cmd.limit = this._limit;;

...

function(x) // prep cmd...

// run commandvar res = this._db.runCommand(cmd);;

// return result...

> db.runCommand(count : "foo")

"n" : 40,"ok" : 1

>

> db.runCommand(listCommands : 1)

> db.runCommand(listCommands : 1)

"commands" : "$eval" : ...,"assertinfo : ...,"authenticate" : ...,"buildinfo" : ...,"clean" : ...,"clone" : ...,"cloneCollection" : ...,"cloneCollectionAsCapped" : ...,"closeAllDatabases" : ...,...

> db.runCommand(... datasize : collectionName)

> db.runCommand(... datasize : collectionName)

> db.runCommand(... dbstats : 1)

> db.commandHelp("distinct")help for: distinct:

distinct : 'collection name',key : 'a.b'

>

> db.commandHelp("distinct")help for: distinct:

distinct : 'collection name',key : 'a.b'

> db.commandHelpfunction(name)

var c = ;;c[name] = 1;;c.help = true;;return this.runCommand(c).help;;

>

> db.runCommand("count" : 1, ... "help" : true)

> db.runCommand("count" : 1, ... "help" : true)help for: count: no help defined>

$

> db.blog.posts.find(... _id : 123,... comments : 1)

> db.blog.posts.find(... _id : 123,... comments : $slice : 10)

> db.blog.posts.find(... _id : 123,... comments : $slice : [20, 10])

> db.blog.posts.find(... _id : 123,... comments : $slice : -­10)

> db.blog.posts.find(... _id : 123,... comments : $slice : [-­20, 10])

> db.blog.posts.find(... comments : ... date : $gt : today,... votes : $gte : 10... )

> db.blog.posts.find(... 'comments.date' : $gt : today,... 'comments.votes' : $gte : 10... )

> db.blog.posts.find(... comments : $elemMatch : ... date : $gt : today,... votes : $gte : 10... )

Types

minKey

maxKey

MapReduce

_id : 123,name : "Fred",email : "fred@example.com"

_id : 456,name : "Paul",phone : "555-­5555",picture : BinData

_id : 789,email : "alice@example.com",

_id 40name 32email 20phone 2picture 17pet 3friends 40hobbies 6favorites 12notes 14

_id : 123,name : "Fred",email : "fred@example.com"

1

11

1

1

1

_id

name

email

email

email

12

12

cluck cluck

email

12

map = function() for (var key in this) emit(key, 1);;

reduce = function(prev, current) var count = 0;;for (var egg in current) count += current[egg];;

return count;;

EB D FA C

EB D FA C

_id : C, value :

_id : F, value :

_id : E, value :_id : A, value :

EB D FA C

_id : B, value : _id : D, value :

GridFS

Indexes

> db.users.find(... date : new Date("10/3/2010"))

> db.users.ensureIndex(... date : 1)

Dates, timestamps, ObjectIds

Aaron @ 3:15Niantic Room

indexing

Geospatial Indexes

> db.map.ensureIndex(location : "2d")>

> db.map.insert(location : [-­40, 78])>

> db.map.insert(location : [-­40, 78])> db.map.insert(location : ... x : -­40, ... y : 78)>

> db.map.insert(location : [-­40, 78])> db.map.insert(location : ... x : -­40, ... y : 78)> db.map.insert(location : ... longitude : -­40, ... latitude : 78)>

> db.map.find(location : ... $near : [-­40, 77])

> db.map.find(location : ... $near : [-­40, 77])-­>limit(10)

> db.map.ensureIndex(... location : "2d",... user : 1);;

1 degree of longitude at the equator:

1 degree of longitude at the equator:

1 degree of longitude near the poles:

Eliot @ 10:15Gateway Room

geospatial

please help document stuff!

http://cookbook.mongodb.org

want to work on mongodb?

http://www.10gen.com/jobs

thank you!

@kchodorow

http://www.snailinaturtleneck.com

Recommended