SlideShare a Scribd company logo
1 of 12
ObjectID in MongoDB
                            Tips & tricks



                                            Vlad ZLOTEANU
     #ParisRB                    Software Engineer @ Dimelo
     Nov 12, 2012
                                              @vladzloteanu


Copyright Dimelo SA                                www.dimelo.com
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
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
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
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
?



        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
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
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
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
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
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
Thank you!




Copyright Dimelo SA     www.dimelo.com

More Related Content

Similar to ObjectId in Mongodb

Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDBNorberto Leite
 
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBJava Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBTobias Trelle
 
Clean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflixClean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflixVictor Rentea
 
Elastic @ Adobe: Making Search Smarter with Machine Learning at Scale
Elastic @ Adobe: Making Search Smarter with Machine Learning at ScaleElastic @ Adobe: Making Search Smarter with Machine Learning at Scale
Elastic @ Adobe: Making Search Smarter with Machine Learning at ScaleElasticsearch
 
Tokens and Complex Systems
Tokens and Complex SystemsTokens and Complex Systems
Tokens and Complex SystemsTrent McConaghy
 
Flying a Drone with JavaScript and Computer Vision
Flying a Drone with JavaScript and Computer VisionFlying a Drone with JavaScript and Computer Vision
Flying a Drone with JavaScript and Computer VisionIvo Andreev
 
Content Mangement Systems and MongoDB
Content Mangement Systems and MongoDBContent Mangement Systems and MongoDB
Content Mangement Systems and MongoDBMitch Pirtle
 
IndexedDB and Push Notifications in Progressive Web Apps
IndexedDB and Push Notifications in Progressive Web AppsIndexedDB and Push Notifications in Progressive Web Apps
IndexedDB and Push Notifications in Progressive Web AppsAdégòkè Obasá
 
DDD, CQRS & ES lessons learned (Gitte Vermeiren)
DDD, CQRS & ES lessons learned (Gitte Vermeiren)DDD, CQRS & ES lessons learned (Gitte Vermeiren)
DDD, CQRS & ES lessons learned (Gitte Vermeiren)Visug
 
moma-django overview --> Django + MongoDB: building a custom ORM layer
moma-django overview --> Django + MongoDB: building a custom ORM layermoma-django overview --> Django + MongoDB: building a custom ORM layer
moma-django overview --> Django + MongoDB: building a custom ORM layerGadi Oren
 
Introduction To Dojo
Introduction To DojoIntroduction To Dojo
Introduction To Dojoyoavrubin
 
Data Mining Open Ap Is
Data Mining Open Ap IsData Mining Open Ap Is
Data Mining Open Ap Isoscon2007
 
MongoDB .local Paris 2020: Les bonnes pratiques pour travailler avec les donn...
MongoDB .local Paris 2020: Les bonnes pratiques pour travailler avec les donn...MongoDB .local Paris 2020: Les bonnes pratiques pour travailler avec les donn...
MongoDB .local Paris 2020: Les bonnes pratiques pour travailler avec les donn...MongoDB
 
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB
 
Tic tac toe with IBM DevOps
Tic tac toe with IBM DevOpsTic tac toe with IBM DevOps
Tic tac toe with IBM DevOpsShaily Dubey
 
The Art of Clean code
The Art of Clean codeThe Art of Clean code
The Art of Clean codeVictor Rentea
 

Similar to ObjectId in Mongodb (20)

Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDB
 
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBJava Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
 
Clean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflixClean pragmatic architecture @ devflix
Clean pragmatic architecture @ devflix
 
DDD for real
DDD for realDDD for real
DDD for real
 
Elastic @ Adobe: Making Search Smarter with Machine Learning at Scale
Elastic @ Adobe: Making Search Smarter with Machine Learning at ScaleElastic @ Adobe: Making Search Smarter with Machine Learning at Scale
Elastic @ Adobe: Making Search Smarter with Machine Learning at Scale
 
Tokens and Complex Systems
Tokens and Complex SystemsTokens and Complex Systems
Tokens and Complex Systems
 
Flying a Drone with JavaScript and Computer Vision
Flying a Drone with JavaScript and Computer VisionFlying a Drone with JavaScript and Computer Vision
Flying a Drone with JavaScript and Computer Vision
 
Content Mangement Systems and MongoDB
Content Mangement Systems and MongoDBContent Mangement Systems and MongoDB
Content Mangement Systems and MongoDB
 
IndexedDB and Push Notifications in Progressive Web Apps
IndexedDB and Push Notifications in Progressive Web AppsIndexedDB and Push Notifications in Progressive Web Apps
IndexedDB and Push Notifications in Progressive Web Apps
 
DDD, CQRS & ES lessons learned (Gitte Vermeiren)
DDD, CQRS & ES lessons learned (Gitte Vermeiren)DDD, CQRS & ES lessons learned (Gitte Vermeiren)
DDD, CQRS & ES lessons learned (Gitte Vermeiren)
 
moma-django overview --> Django + MongoDB: building a custom ORM layer
moma-django overview --> Django + MongoDB: building a custom ORM layermoma-django overview --> Django + MongoDB: building a custom ORM layer
moma-django overview --> Django + MongoDB: building a custom ORM layer
 
Introduction To Dojo
Introduction To DojoIntroduction To Dojo
Introduction To Dojo
 
Data Mining Open Ap Is
Data Mining Open Ap IsData Mining Open Ap Is
Data Mining Open Ap Is
 
MongoDB .local Paris 2020: Les bonnes pratiques pour travailler avec les donn...
MongoDB .local Paris 2020: Les bonnes pratiques pour travailler avec les donn...MongoDB .local Paris 2020: Les bonnes pratiques pour travailler avec les donn...
MongoDB .local Paris 2020: Les bonnes pratiques pour travailler avec les donn...
 
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
 
Tic tac toe with IBM DevOps
Tic tac toe with IBM DevOpsTic tac toe with IBM DevOps
Tic tac toe with IBM DevOps
 
The Art of Clean code
The Art of Clean codeThe Art of Clean code
The Art of Clean code
 
Green dao
Green daoGreen dao
Green dao
 
MongoDB for Developers
MongoDB for DevelopersMongoDB for Developers
MongoDB for Developers
 
Plone pwns
Plone pwnsPlone pwns
Plone pwns
 

Recently uploaded

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 

Recently uploaded (20)

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 

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
  • 12. Thank you! Copyright Dimelo SA www.dimelo.com