SlideShare a Scribd company logo
1 of 62
Modeling Data
 in MongoDB
   Luke Ehresman



   http://copperegg.com
Schema Design
Schema Design

Wait, isn’t MongoDB schemaless?
Schema Design

Wait, isn’t MongoDB schemaless?

         Nope!
   (just no predefined schema)
Schema Design

Wait, isn’t MongoDB schemaless?

            Nope!
    (just no predefined schema)

That means it’s up to your application.
Schema Design
    (Relational)
Schema Design
           (Relational)

• Tabular data - Tables, Rows, Columns
Schema Design
           (Relational)

• Tabular data - Tables, Rows, Columns
• Normalized - flatten your data
Schema Design
           (Relational)

• Tabular data - Tables, Rows, Columns
• Normalized - flatten your data
• Columns with simple values (int, varchar)
Schema Design
           (Relational)

• Tabular data - Tables, Rows, Columns
• Normalized - flatten your data
• Columns with simple values (int, varchar)
• Relate rows with foreign key references
Schema Design
           (Relational)

• Tabular data - Tables, Rows, Columns
• Normalized - flatten your data
• Columns with simple values (int, varchar)
• Relate rows with foreign key references
• Reuse, don’t repeat (i.e. person)
Schema Design
           (Relational)

• Tabular data - Tables, Rows, Columns
• Normalized - flatten your data
• Columns with simple values (int, varchar)
• Relate rows with foreign key references
• Reuse, don’t repeat (i.e. person)
• Indexes on values
Schema Design
 (MongoDB - Non-Relational)
Schema Design
       (MongoDB - Non-Relational)

• Databases > Collections > Documents
Schema Design
       (MongoDB - Non-Relational)

• Databases > Collections > Documents
• Simple or complex values
   (ints, strings, objects, arrays)
Schema Design
       (MongoDB - Non-Relational)

• Databases > Collections > Documents
• Simple or complex values
   (ints, strings, objects, arrays)
• Documents are monolithic units
Schema Design
       (MongoDB - Non-Relational)

• Databases > Collections > Documents
• Simple or complex values
   (ints, strings, objects, arrays)
• Documents are monolithic units
• Embedded complex data structures
Schema Design
        (MongoDB - Non-Relational)

• Databases > Collections > Documents
• Simple or complex values
    (ints, strings, objects, arrays)
• Documents are monolithic units
• Embedded complex data structures
• No joins - repeat data for faster access
Schema Design
        (MongoDB - Non-Relational)

• Databases > Collections > Documents
• Simple or complex values
    (ints, strings, objects, arrays)
• Documents are monolithic units
• Embedded complex data structures
• No joins - repeat data for faster access
• Difficult to relate documents together
How will you use it?
How will you use it?
• The best way to use MongoDB is to tailor
  your schema to how it will be used
How will you use it?
• The best way to use MongoDB is to tailor
  your schema to how it will be used
• Things to consider:
How will you use it?
• The best way to use MongoDB is to tailor
  your schema to how it will be used
• Things to consider:
 • minimize reads and/or writes
How will you use it?
• The best way to use MongoDB is to tailor
  your schema to how it will be used
• Things to consider:
 • minimize reads and/or writes
 • more writes, fewer reads? (read heavy)
How will you use it?
• The best way to use MongoDB is to tailor
  your schema to how it will be used
• Things to consider:
 • minimize reads and/or writes
 • more writes, fewer reads? (read heavy)
 • more reads, fewer writes? (write heavy)
How will you use it?
How will you use it?
• Combine objects into one document if you
  will use them together.
How will you use it?
• Combine objects into one document if you
  will use them together.
• Example: Authors and Books
How will you use it?
• Combine objects into one document if you
  will use them together.
• Example: Authors and Books
• Separate them if they need to be used
  separately -- but beware, no joins!
How will you use it?
• Combine objects into one document if you
  will use them together.
• Example: Authors and Books
• Separate them if they need to be used
  separately -- but beware, no joins!
• Or duplicate the data -- but beware!
Precompute!
Precompute!
• Philosophy: do work before reads occur
Precompute!
• Philosophy: do work before reads occur
• Disk space is cheap - compute time is not
     (it’s expensive because users wait)
Precompute!
• Philosophy: do work before reads occur
• Disk space is cheap - compute time is not
     (it’s expensive because users wait)
• Do joins on write, not on read
Precompute!
• Philosophy: do work before reads occur
• Disk space is cheap - compute time is not
     (it’s expensive because users wait)
• Do joins on write, not on read
• Do complex aggregation ahead of time
Precompute!
• Philosophy: do work before reads occur
• Disk space is cheap - compute time is not
     (it’s expensive because users wait)
• Do joins on write, not on read
• Do complex aggregation ahead of time
• Optimize for specific use cases
Precompute!
• Philosophy: do work before reads occur
• Disk space is cheap - compute time is not
     (it’s expensive because users wait)
• Do joins on write, not on read
• Do complex aggregation ahead of time
• Optimize for specific use cases
• Delayed data is not always bad in real life
Aggregation
Aggregation

• Application
Aggregation

• Application
• MapReduce (BEWARE!)
Aggregation

• Application
• MapReduce (BEWARE!)
• Group
Aggregation

• Application
• MapReduce (BEWARE!)
• Group
• Aggregation framework (coming in 2.2)
Atomicity
Atomicity

• MongoDB does have atomic transactions
Atomicity

• MongoDB does have atomic transactions
• Scope is a single document
Atomicity

• MongoDB does have atomic transactions
• Scope is a single document
• Keep this in mind when designing schemas
Atomicity
Atomicity

• $inc
Atomicity

• $inc
• $push
Atomicity

• $inc
• $push
• $addToSet
Atomicity

• $inc
• $push
• $addToSet
• upsert (create-if-none-else-update)
Atomicity
• Upsert example
  db.stats.update({_id: ‘lehresman’},
     {$inc: {logins: 1},
      $set: {last_login: new Date()}},
     true);


• {_id:‘lehresman’, logins:1, last_login:A}
• {_id:‘lehresman’, logins:2, last_login:B}
Example: Books

• Many books
• Many authors
• Authors write many books
Example: Books

                             Bad N oSQL
• Many books                  Ex ample!!
• Many authors
• Authors write many books
Example: User Stats


• You have users
• Track what pages they visit
Example: User Stats
“users” collection
{ _id: ‘lehresman’,
  first_name: ‘Luke’,
  last_name: ‘Ehresman’,
  page_visits: {
    ‘/’: 78,
    ‘/profile’: 33,
    ‘/blog/38919’: 2
  }
                   Problem: What if you want
}
                    aggregate stats across users?
Example: User Stats
“visits” collection
{ _id: ‘/’,
  visits: 73889 }

{ _id: ‘/profile’,
  visits: 9341 }

{ _id: ‘/blog/38919’
  visits: 1678 }
Example: User Stats
“visits” collection
{ _id: ‘/’,
  visits: 73889 }

{ _id: ‘/profile’,
  visits: 9341 }

{ _id: ‘/blog/38919’        Problems:
  visits: 1678 }         No user tracking;
                         What if you want
                       aggregate stats by day?
Example: User Stats
“visits” collection
{ _id: ‘/’,
  visits: 73889,
  { ‘2012-06-01’: 839,
    ‘2012-06-02’: 767,
    ‘2012-06-03’: 881 }
Example: User Stats
“visits” collection
{ _id: ‘/’,
  visits: 73889,
  { ‘2012-06-01’: 839,
    ‘2012-06-02’: 767,
    ‘2012-06-03’: 881 }

             Problems: No user tracking;
              Possibly too large eventually.
                     Always grows.
Example: User Stats
“visits” collection
{ date: ‘2012-06-01’,
  page: ‘/’,
  visits: 839,
  users: {
    ‘lehresman’: 78,
    ‘billybob’: 761
  }
}
Example: User Stats
“visits” collection
{ date: ‘2012-06-01’,
  page: ‘/’,
  visits: 839,
  users: {
    ‘lehresman’: 78,
    ‘billybob’: 761
  }
}
             No relational integrity.
   (up to your application to handle null cases)

More Related Content

What's hot

Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbAlex Sharp
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMike Friedman
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsWebinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsMongoDB
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosConceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosMongoDB
 
Socialite, the Open Source Status Feed
Socialite, the Open Source Status FeedSocialite, the Open Source Status Feed
Socialite, the Open Source Status FeedMongoDB
 
Back to Basics Webinar 1: Introduction to NoSQL
Back to Basics Webinar 1: Introduction to NoSQLBack to Basics Webinar 1: Introduction to NoSQL
Back to Basics Webinar 1: Introduction to NoSQLMongoDB
 
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
Socialite, the Open Source Status Feed Part 3: Scaling the Data FeedSocialite, the Open Source Status Feed Part 3: Scaling the Data Feed
Socialite, the Open Source Status Feed Part 3: Scaling the Data FeedMongoDB
 
Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
 Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
Conceptos básicos. Seminario web 2: Su primera aplicación MongoDBMongoDB
 
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...MongoDB
 
5 Pitfalls to Avoid with MongoDB
5 Pitfalls to Avoid with MongoDB5 Pitfalls to Avoid with MongoDB
5 Pitfalls to Avoid with MongoDBTim Callaghan
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleMongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBantoinegirbal
 
User Data Management with MongoDB
User Data Management with MongoDB User Data Management with MongoDB
User Data Management with MongoDB MongoDB
 
Socialite, the Open Source Status Feed Part 1: Design Overview and Scaling fo...
Socialite, the Open Source Status Feed Part 1: Design Overview and Scaling fo...Socialite, the Open Source Status Feed Part 1: Design Overview and Scaling fo...
Socialite, the Open Source Status Feed Part 1: Design Overview and Scaling fo...MongoDB
 
2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introductionantoinegirbal
 
MongoDB Schema Design by Examples
MongoDB Schema Design by ExamplesMongoDB Schema Design by Examples
MongoDB Schema Design by ExamplesHadi Ariawan
 

What's hot (20)

MongoDB and Schema Design
MongoDB and Schema DesignMongoDB and Schema Design
MongoDB and Schema Design
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo Db
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World Examples
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsWebinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in Documents
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosConceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
 
Socialite, the Open Source Status Feed
Socialite, the Open Source Status FeedSocialite, the Open Source Status Feed
Socialite, the Open Source Status Feed
 
Back to Basics Webinar 1: Introduction to NoSQL
Back to Basics Webinar 1: Introduction to NoSQLBack to Basics Webinar 1: Introduction to NoSQL
Back to Basics Webinar 1: Introduction to NoSQL
 
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
Socialite, the Open Source Status Feed Part 3: Scaling the Data FeedSocialite, the Open Source Status Feed Part 3: Scaling the Data Feed
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
 
Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
 Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
Conceptos básicos. Seminario web 2: Su primera aplicación MongoDB
 
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
 
MongoDB
MongoDBMongoDB
MongoDB
 
5 Pitfalls to Avoid with MongoDB
5 Pitfalls to Avoid with MongoDB5 Pitfalls to Avoid with MongoDB
5 Pitfalls to Avoid with MongoDB
 
Indexing Strategies to Help You Scale
Indexing Strategies to Help You ScaleIndexing Strategies to Help You Scale
Indexing Strategies to Help You Scale
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
User Data Management with MongoDB
User Data Management with MongoDB User Data Management with MongoDB
User Data Management with MongoDB
 
MongoDB 101
MongoDB 101MongoDB 101
MongoDB 101
 
Socialite, the Open Source Status Feed Part 1: Design Overview and Scaling fo...
Socialite, the Open Source Status Feed Part 1: Design Overview and Scaling fo...Socialite, the Open Source Status Feed Part 1: Design Overview and Scaling fo...
Socialite, the Open Source Status Feed Part 1: Design Overview and Scaling fo...
 
2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction2011 Mongo FR - MongoDB introduction
2011 Mongo FR - MongoDB introduction
 
MongoDB Schema Design by Examples
MongoDB Schema Design by ExamplesMongoDB Schema Design by Examples
MongoDB Schema Design by Examples
 
Mongo db operations_v2
Mongo db operations_v2Mongo db operations_v2
Mongo db operations_v2
 

Viewers also liked

Building a Social Network with MongoDB
  Building a Social Network with MongoDB  Building a Social Network with MongoDB
Building a Social Network with MongoDBFred Chu
 
Common MongoDB Use Cases
Common MongoDB Use Cases Common MongoDB Use Cases
Common MongoDB Use Cases MongoDB
 
MongoDB Advanced Schema Design - Inboxes
MongoDB Advanced Schema Design - InboxesMongoDB Advanced Schema Design - Inboxes
MongoDB Advanced Schema Design - InboxesJared Rosoff
 
The Right (and Wrong) Use Cases for MongoDB
The Right (and Wrong) Use Cases for MongoDBThe Right (and Wrong) Use Cases for MongoDB
The Right (and Wrong) Use Cases for MongoDBMongoDB
 
Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDBrogerbodamer
 
DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - ...
DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - ...DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - ...
DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - ...NoSQLmatters
 
Apache Cassandra in the Real World
Apache Cassandra in the Real WorldApache Cassandra in the Real World
Apache Cassandra in the Real WorldJeremy Hanna
 
Apache Cassandra in the Real World
Apache Cassandra in the Real WorldApache Cassandra in the Real World
Apache Cassandra in the Real WorldJeremy Hanna
 
MongoDB for Time Series Data: Schema Design
MongoDB for Time Series Data: Schema DesignMongoDB for Time Series Data: Schema Design
MongoDB for Time Series Data: Schema DesignMongoDB
 
MongoDB for Time Series Data
MongoDB for Time Series DataMongoDB for Time Series Data
MongoDB for Time Series DataMongoDB
 
The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js
The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.jsThe MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js
The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.jsMongoDB
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...MongoDB
 
Internet of Things and Big Data: Vision and Concrete Use Cases
Internet of Things and Big Data: Vision and Concrete Use CasesInternet of Things and Big Data: Vision and Concrete Use Cases
Internet of Things and Big Data: Vision and Concrete Use CasesMongoDB
 
Back to Basics Webinar 1: Introduction to NoSQL
Back to Basics Webinar 1: Introduction to NoSQLBack to Basics Webinar 1: Introduction to NoSQL
Back to Basics Webinar 1: Introduction to NoSQLMongoDB
 
Webinar: Right and Wrong Ways to Implement MongoDB
Webinar: Right and Wrong Ways to Implement MongoDBWebinar: Right and Wrong Ways to Implement MongoDB
Webinar: Right and Wrong Ways to Implement MongoDBMongoDB
 
MongoDB hearts Django? (Django NYC)
MongoDB hearts Django? (Django NYC)MongoDB hearts Django? (Django NYC)
MongoDB hearts Django? (Django NYC)Mike Dirolf
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Designaaronheckmann
 
Schema Design
Schema DesignSchema Design
Schema DesignMongoDB
 
Schema Design at Scale
Schema Design at ScaleSchema Design at Scale
Schema Design at ScaleRick Copeland
 
Mango Database - Web Development
Mango Database - Web DevelopmentMango Database - Web Development
Mango Database - Web Developmentmssaman
 

Viewers also liked (20)

Building a Social Network with MongoDB
  Building a Social Network with MongoDB  Building a Social Network with MongoDB
Building a Social Network with MongoDB
 
Common MongoDB Use Cases
Common MongoDB Use Cases Common MongoDB Use Cases
Common MongoDB Use Cases
 
MongoDB Advanced Schema Design - Inboxes
MongoDB Advanced Schema Design - InboxesMongoDB Advanced Schema Design - Inboxes
MongoDB Advanced Schema Design - Inboxes
 
The Right (and Wrong) Use Cases for MongoDB
The Right (and Wrong) Use Cases for MongoDBThe Right (and Wrong) Use Cases for MongoDB
The Right (and Wrong) Use Cases for MongoDB
 
Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDB
 
DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - ...
DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - ...DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - ...
DOAN DuyHai – Cassandra: real world best use-cases and worst anti-patterns - ...
 
Apache Cassandra in the Real World
Apache Cassandra in the Real WorldApache Cassandra in the Real World
Apache Cassandra in the Real World
 
Apache Cassandra in the Real World
Apache Cassandra in the Real WorldApache Cassandra in the Real World
Apache Cassandra in the Real World
 
MongoDB for Time Series Data: Schema Design
MongoDB for Time Series Data: Schema DesignMongoDB for Time Series Data: Schema Design
MongoDB for Time Series Data: Schema Design
 
MongoDB for Time Series Data
MongoDB for Time Series DataMongoDB for Time Series Data
MongoDB for Time Series Data
 
The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js
The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.jsThe MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js
The MEAN Stack: MongoDB, ExpressJS, AngularJS and Node.js
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
 
Internet of Things and Big Data: Vision and Concrete Use Cases
Internet of Things and Big Data: Vision and Concrete Use CasesInternet of Things and Big Data: Vision and Concrete Use Cases
Internet of Things and Big Data: Vision and Concrete Use Cases
 
Back to Basics Webinar 1: Introduction to NoSQL
Back to Basics Webinar 1: Introduction to NoSQLBack to Basics Webinar 1: Introduction to NoSQL
Back to Basics Webinar 1: Introduction to NoSQL
 
Webinar: Right and Wrong Ways to Implement MongoDB
Webinar: Right and Wrong Ways to Implement MongoDBWebinar: Right and Wrong Ways to Implement MongoDB
Webinar: Right and Wrong Ways to Implement MongoDB
 
MongoDB hearts Django? (Django NYC)
MongoDB hearts Django? (Django NYC)MongoDB hearts Django? (Django NYC)
MongoDB hearts Django? (Django NYC)
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Schema Design at Scale
Schema Design at ScaleSchema Design at Scale
Schema Design at Scale
 
Mango Database - Web Development
Mango Database - Web DevelopmentMango Database - Web Development
Mango Database - Web Development
 

Similar to Modeling Data in MongoDB

Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBSean Laurent
 
Sharing a Startup’s Big Data Lessons
Sharing a Startup’s Big Data LessonsSharing a Startup’s Big Data Lessons
Sharing a Startup’s Big Data LessonsGeorge Stathis
 
Graph Databases
Graph DatabasesGraph Databases
Graph Databasesthai
 
Demystifying data engineering
Demystifying data engineeringDemystifying data engineering
Demystifying data engineeringThang Bui (Bob)
 
"R, Hadoop, and Amazon Web Services (20 December 2011)"
"R, Hadoop, and Amazon Web Services (20 December 2011)""R, Hadoop, and Amazon Web Services (20 December 2011)"
"R, Hadoop, and Amazon Web Services (20 December 2011)"Portland R User Group
 
MongoDB: What, why, when
MongoDB: What, why, whenMongoDB: What, why, when
MongoDB: What, why, whenEugenio Minardi
 
MongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behlMongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behlTO THE NEW | Technology
 
Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016Mukesh Tilokani
 
Building better SQL Server Databases
Building better SQL Server DatabasesBuilding better SQL Server Databases
Building better SQL Server DatabasesColdFusionConference
 
Advanced Schema Design Patterns
Advanced Schema Design PatternsAdvanced Schema Design Patterns
Advanced Schema Design PatternsMongoDB
 
Webinar: When to Use MongoDB
Webinar: When to Use MongoDBWebinar: When to Use MongoDB
Webinar: When to Use MongoDBMongoDB
 
From 100s to 100s of Millions
From 100s to 100s of MillionsFrom 100s to 100s of Millions
From 100s to 100s of MillionsErik Onnen
 

Similar to Modeling Data in MongoDB (20)

Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
NoSQL
NoSQLNoSQL
NoSQL
 
MongoDB Basics
MongoDB BasicsMongoDB Basics
MongoDB Basics
 
Sharing a Startup’s Big Data Lessons
Sharing a Startup’s Big Data LessonsSharing a Startup’s Big Data Lessons
Sharing a Startup’s Big Data Lessons
 
Graph Databases
Graph DatabasesGraph Databases
Graph Databases
 
Demystifying data engineering
Demystifying data engineeringDemystifying data engineering
Demystifying data engineering
 
No sql Database
No sql DatabaseNo sql Database
No sql Database
 
"R, Hadoop, and Amazon Web Services (20 December 2011)"
"R, Hadoop, and Amazon Web Services (20 December 2011)""R, Hadoop, and Amazon Web Services (20 December 2011)"
"R, Hadoop, and Amazon Web Services (20 December 2011)"
 
R, Hadoop and Amazon Web Services
R, Hadoop and Amazon Web ServicesR, Hadoop and Amazon Web Services
R, Hadoop and Amazon Web Services
 
NoSql
NoSqlNoSql
NoSql
 
MongoDB: What, why, when
MongoDB: What, why, whenMongoDB: What, why, when
MongoDB: What, why, when
 
MongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behlMongoDB using Grails plugin by puneet behl
MongoDB using Grails plugin by puneet behl
 
Mongo DB
Mongo DB Mongo DB
Mongo DB
 
Scalable web architecture
Scalable web architectureScalable web architecture
Scalable web architecture
 
Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016Tech Gupshup Meetup On MongoDB - 24/06/2016
Tech Gupshup Meetup On MongoDB - 24/06/2016
 
Building better SQL Server Databases
Building better SQL Server DatabasesBuilding better SQL Server Databases
Building better SQL Server Databases
 
Advanced Schema Design Patterns
Advanced Schema Design PatternsAdvanced Schema Design Patterns
Advanced Schema Design Patterns
 
Webinar: When to Use MongoDB
Webinar: When to Use MongoDBWebinar: When to Use MongoDB
Webinar: When to Use MongoDB
 
NoSQL and MongoDB
NoSQL and MongoDBNoSQL and MongoDB
NoSQL and MongoDB
 
From 100s to 100s of Millions
From 100s to 100s of MillionsFrom 100s to 100s of Millions
From 100s to 100s of Millions
 

Recently uploaded

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 

Recently uploaded (20)

WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 

Modeling Data in MongoDB

  • 1. Modeling Data in MongoDB Luke Ehresman http://copperegg.com
  • 3. Schema Design Wait, isn’t MongoDB schemaless?
  • 4. Schema Design Wait, isn’t MongoDB schemaless? Nope! (just no predefined schema)
  • 5. Schema Design Wait, isn’t MongoDB schemaless? Nope! (just no predefined schema) That means it’s up to your application.
  • 6. Schema Design (Relational)
  • 7. Schema Design (Relational) • Tabular data - Tables, Rows, Columns
  • 8. Schema Design (Relational) • Tabular data - Tables, Rows, Columns • Normalized - flatten your data
  • 9. Schema Design (Relational) • Tabular data - Tables, Rows, Columns • Normalized - flatten your data • Columns with simple values (int, varchar)
  • 10. Schema Design (Relational) • Tabular data - Tables, Rows, Columns • Normalized - flatten your data • Columns with simple values (int, varchar) • Relate rows with foreign key references
  • 11. Schema Design (Relational) • Tabular data - Tables, Rows, Columns • Normalized - flatten your data • Columns with simple values (int, varchar) • Relate rows with foreign key references • Reuse, don’t repeat (i.e. person)
  • 12. Schema Design (Relational) • Tabular data - Tables, Rows, Columns • Normalized - flatten your data • Columns with simple values (int, varchar) • Relate rows with foreign key references • Reuse, don’t repeat (i.e. person) • Indexes on values
  • 13. Schema Design (MongoDB - Non-Relational)
  • 14. Schema Design (MongoDB - Non-Relational) • Databases > Collections > Documents
  • 15. Schema Design (MongoDB - Non-Relational) • Databases > Collections > Documents • Simple or complex values (ints, strings, objects, arrays)
  • 16. Schema Design (MongoDB - Non-Relational) • Databases > Collections > Documents • Simple or complex values (ints, strings, objects, arrays) • Documents are monolithic units
  • 17. Schema Design (MongoDB - Non-Relational) • Databases > Collections > Documents • Simple or complex values (ints, strings, objects, arrays) • Documents are monolithic units • Embedded complex data structures
  • 18. Schema Design (MongoDB - Non-Relational) • Databases > Collections > Documents • Simple or complex values (ints, strings, objects, arrays) • Documents are monolithic units • Embedded complex data structures • No joins - repeat data for faster access
  • 19. Schema Design (MongoDB - Non-Relational) • Databases > Collections > Documents • Simple or complex values (ints, strings, objects, arrays) • Documents are monolithic units • Embedded complex data structures • No joins - repeat data for faster access • Difficult to relate documents together
  • 20. How will you use it?
  • 21. How will you use it? • The best way to use MongoDB is to tailor your schema to how it will be used
  • 22. How will you use it? • The best way to use MongoDB is to tailor your schema to how it will be used • Things to consider:
  • 23. How will you use it? • The best way to use MongoDB is to tailor your schema to how it will be used • Things to consider: • minimize reads and/or writes
  • 24. How will you use it? • The best way to use MongoDB is to tailor your schema to how it will be used • Things to consider: • minimize reads and/or writes • more writes, fewer reads? (read heavy)
  • 25. How will you use it? • The best way to use MongoDB is to tailor your schema to how it will be used • Things to consider: • minimize reads and/or writes • more writes, fewer reads? (read heavy) • more reads, fewer writes? (write heavy)
  • 26. How will you use it?
  • 27. How will you use it? • Combine objects into one document if you will use them together.
  • 28. How will you use it? • Combine objects into one document if you will use them together. • Example: Authors and Books
  • 29. How will you use it? • Combine objects into one document if you will use them together. • Example: Authors and Books • Separate them if they need to be used separately -- but beware, no joins!
  • 30. How will you use it? • Combine objects into one document if you will use them together. • Example: Authors and Books • Separate them if they need to be used separately -- but beware, no joins! • Or duplicate the data -- but beware!
  • 32. Precompute! • Philosophy: do work before reads occur
  • 33. Precompute! • Philosophy: do work before reads occur • Disk space is cheap - compute time is not (it’s expensive because users wait)
  • 34. Precompute! • Philosophy: do work before reads occur • Disk space is cheap - compute time is not (it’s expensive because users wait) • Do joins on write, not on read
  • 35. Precompute! • Philosophy: do work before reads occur • Disk space is cheap - compute time is not (it’s expensive because users wait) • Do joins on write, not on read • Do complex aggregation ahead of time
  • 36. Precompute! • Philosophy: do work before reads occur • Disk space is cheap - compute time is not (it’s expensive because users wait) • Do joins on write, not on read • Do complex aggregation ahead of time • Optimize for specific use cases
  • 37. Precompute! • Philosophy: do work before reads occur • Disk space is cheap - compute time is not (it’s expensive because users wait) • Do joins on write, not on read • Do complex aggregation ahead of time • Optimize for specific use cases • Delayed data is not always bad in real life
  • 42. Aggregation • Application • MapReduce (BEWARE!) • Group • Aggregation framework (coming in 2.2)
  • 44. Atomicity • MongoDB does have atomic transactions
  • 45. Atomicity • MongoDB does have atomic transactions • Scope is a single document
  • 46. Atomicity • MongoDB does have atomic transactions • Scope is a single document • Keep this in mind when designing schemas
  • 51. Atomicity • $inc • $push • $addToSet • upsert (create-if-none-else-update)
  • 52. Atomicity • Upsert example db.stats.update({_id: ‘lehresman’}, {$inc: {logins: 1}, $set: {last_login: new Date()}}, true); • {_id:‘lehresman’, logins:1, last_login:A} • {_id:‘lehresman’, logins:2, last_login:B}
  • 53. Example: Books • Many books • Many authors • Authors write many books
  • 54. Example: Books Bad N oSQL • Many books Ex ample!! • Many authors • Authors write many books
  • 55. Example: User Stats • You have users • Track what pages they visit
  • 56. Example: User Stats “users” collection { _id: ‘lehresman’, first_name: ‘Luke’, last_name: ‘Ehresman’, page_visits: { ‘/’: 78, ‘/profile’: 33, ‘/blog/38919’: 2 } Problem: What if you want } aggregate stats across users?
  • 57. Example: User Stats “visits” collection { _id: ‘/’, visits: 73889 } { _id: ‘/profile’, visits: 9341 } { _id: ‘/blog/38919’ visits: 1678 }
  • 58. Example: User Stats “visits” collection { _id: ‘/’, visits: 73889 } { _id: ‘/profile’, visits: 9341 } { _id: ‘/blog/38919’ Problems: visits: 1678 } No user tracking; What if you want aggregate stats by day?
  • 59. Example: User Stats “visits” collection { _id: ‘/’, visits: 73889, { ‘2012-06-01’: 839, ‘2012-06-02’: 767, ‘2012-06-03’: 881 }
  • 60. Example: User Stats “visits” collection { _id: ‘/’, visits: 73889, { ‘2012-06-01’: 839, ‘2012-06-02’: 767, ‘2012-06-03’: 881 } Problems: No user tracking; Possibly too large eventually. Always grows.
  • 61. Example: User Stats “visits” collection { date: ‘2012-06-01’, page: ‘/’, visits: 839, users: { ‘lehresman’: 78, ‘billybob’: 761 } }
  • 62. Example: User Stats “visits” collection { date: ‘2012-06-01’, page: ‘/’, visits: 839, users: { ‘lehresman’: 78, ‘billybob’: 761 } } No relational integrity. (up to your application to handle null cases)

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n