Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
ObjectID in MongoDB                            Tips & tricks                                            Vlad ZLOTEANU     ...
Poll           How many:                 Using mongo in prod                 Using mongo on personal projects             ...
ObjectID – facts           any (mongo) document has an _id attribute              (exception: capped collections, system  ...
ObjectID – interesting facts     it is usually a BSON ObjectID          but it can be any object (except for: arrays), as ...
BSON ObjectID                      0   1   2   3   4   5   6   7   8   9 10 11                          Time        Machin...
?        relational DB: sequence numbers (incremental) as      ID, generated by DB        mongoDB: pseudo-incremental obje...
Tip: extract generation time           granularity: 1s           optimize space  no more need for created_at field    bso...
Tip: range query by date           ! granularity: 1s            no more need for index on created_at    first_object_id =...
Tip: sort by date           ! 1s precision search (cvasi-inconsistent order)           no more need for index on created_a...
Tip: ObjectID as compound index      Stats scenario: count hits, per page, per hour  {       _id: "{day}/{page}", # “20120...
Tip: compound index [2]           only one upsert (in-place change) using default           index, no need for extra index...
Thank you!Copyright Dimelo SA     www.dimelo.com
Upcoming SlideShare
Loading in …5
×

ObjectId in Mongodb

2,168 views

Published on

Published in: Technology
  • Be the first to comment

ObjectId in Mongodb

  1. 1. ObjectID in MongoDB Tips & tricks Vlad ZLOTEANU #ParisRB Software Engineer @ Dimelo Nov 12, 2012 @vladzloteanuCopyright Dimelo SA www.dimelo.com
  2. 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. 3. ObjectID – facts any (mongo) document has an _id attribute (exception: capped collections, system collections) immutable generated automatically by DB if none is providedCopyright Dimelo SA www.dimelo.com
  4. 4. ObjectID – interesting facts it is usually a BSON ObjectID but it can be any object (except for: arrays), as long as its 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 # => 50a13e6a29a86ea0ff000002Copyright Dimelo SA www.dimelo.com
  5. 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 numberCopyright Dimelo SA www.dimelo.com
  6. 6. ? relational DB: sequence numbers (incremental) as ID, generated by DB mongoDB: pseudo-incremental objects, not in sequence, generated client-side Any reason? #parisrb #whymrandersonCopyright Dimelo SA www.dimelo.com
  7. 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 UTCCopyright Dimelo SA www.dimelo.com
  8. 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 # => 1Copyright Dimelo SA www.dimelo.com
  9. 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. 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. 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
  12. 12. Thank you!Copyright Dimelo SA www.dimelo.com

×