Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
ObjectId in Mongodb
1. ObjectID in MongoDB
Tips & tricks
Vlad ZLOTEANU
#ParisRB Software Engineer @ Dimelo
Nov 12, 2012
@vladzloteanu
Copyright Dimelo SA www.dimelo.com
2. Poll
How many:
Using mongo in prod
Using mongo on personal projects
Interested in mongo
What’s “moun-gooh?”
Copyright Dimelo SA www.dimelo.com
3. ObjectID – facts
any (mongo) document has an _id attribute
(exception: capped collections, system
collections)
immutable
generated automatically by DB if none is provided
Copyright Dimelo SA www.dimelo.com
4. ObjectID – interesting facts
it is usually a BSON ObjectID
but it can be any object (except for: arrays), as
long as it's unique
it is usually generated by the driver (on client side)
each collection has a unique index on ObjectID
class Model ; include Mongoid::Document ; end
Model.new.id
# => 50a13e6a29a86ea0ff000002
Copyright Dimelo SA www.dimelo.com
5. BSON ObjectID
0 1 2 3 4 5 6 7 8 9 10 11
Time Machine Pid Inc
Time: Unix timestamp (sec)
Machine: md5 hash of machine name
Pid
Inc: incrementing value starting with a random
number
Copyright Dimelo SA www.dimelo.com
6. ?
relational DB: sequence numbers (incremental) as
ID, generated by DB
mongoDB: pseudo-incremental objects, not in
sequence, generated client-side
Any reason?
#parisrb
#whymranderson
Copyright Dimelo SA www.dimelo.com
7. Tip: extract generation time
granularity: 1s
optimize space no more need for created_at field
bson_id = Model.last.id
bson_id.generation_time
# => 2012-11-12 15:23:45 UTC
Copyright Dimelo SA www.dimelo.com
8. Tip: range query by date
! granularity: 1s
no more need for index on created_at
first_object_id =
Moped::BSON::ObjectId.from_time(1.day.ago)
# => "509fc3d20000000000000000”
Model.where(:_id.gte => first_object_id).count
# => 1
Copyright Dimelo SA www.dimelo.com
9. Tip: sort by date
! 1s precision search (cvasi-inconsistent order)
no more need for index on created_at
Model.asc(:_id)
# instead of: Model.asc(:created_at)
Copyright Dimelo SA www.dimelo.com
10. Tip: ObjectID as compound index
Stats scenario: count hits, per page, per hour
{
_id: "{day}/{page}", # “20120108/index”
hour: {
1: 200
2: 149
…
24: 455
}
total: 2344
}
Copyright Dimelo SA www.dimelo.com
11. Tip: compound index [2]
only one upsert (in-place change) using default
index, no need for extra index
# new hit arrives
conditions = {:_id => "#{hit.day}/#{hit.hour}"}
Stat.collection.find(conditions).upsert({
"$inc" => {"hour.#{hour}" => 1, "total" => 1}})
Copyright Dimelo SA www.dimelo.com