SlideShare a Scribd company logo
1 of 36
Download to read offline
Schema Design
       
Roger Bodamer
 roger@analytica.com
      @rogerb
A brief history of Data Modeling
•  ISAM	

  • COBOL 	

•  Network 	

•  Hiearchical	

•  Relational	

  • 1970 E.F.Codd introduces 1st Normal Form (1NF)	

  • 1971 E.F.Codd introduces 2nd and 3rd Normal Form (2NF, 3NF	

  • 1974 Codd  Boyce define Boyce/Codd Normal Form (BCNF)	

  • 2002 Date, Darween, Lorentzos define 6th Normal Form (6NF)	

• Object
So why model data?
Modeling goals
Goals:	

•  Avoid anomalies when inserting, updating or deleting	

•  Minimize redesign when extending the schema	

•  Make the model informative to users	

•  Avoid bias towards a particular style of query	





                                                       * source : wikipedia
Relational made normalized
data look like this
Document databases make
normalized data look like this
Some terms before we proceed
RDBMS	

           Document DBs	

Table	

           Collection	

View / Row(s)	

   JSON Document	

Index	

           Index	

Join	

            Embedding  Linking across
                   documents	

Partition	

       Shard	

Partition Key	

   Shard Key
Recap

Design documents that simply map to
your application

post	
  =	
  {author:	
   roger ,	
  
	
  	
  	
  	
  	
  	
  	
  	
  date:	
  new	
  Date(),	
  
	
  	
  	
  	
  	
  	
  	
  	
  text:	
   Down	
  Under... ,	
  
	
  	
  	
  	
  	
  	
  	
  	
  tags:	
  [ rockstar , men	
  at	
  work ]}
Query operators

Conditional operators:
       $ne, $in, $nin, $mod, $all, $size, $exists, $type, ..
       $lt, $lte, $gt, $gte, $ne, 

        // find posts with any tags
        db.posts.find({tags: {$exists: true}})


	
  
Query operators

Conditional operators:
       $ne, $in, $nin, $mod, $all, $size, $exists, $type, ..
       $lt, $lte, $gt, $gte, $ne, 

        // find posts with any tags
        db.posts.find({tags: {$exists: true}})

Regular expressions:
         // posts where author starts with k
         db.posts.find({author: /^r*/i }) 

	
  
Query operators

Conditional operators:
       $ne, $in, $nin, $mod, $all, $size, $exists, $type, ..
       $lt, $lte, $gt, $gte, $ne, 

        // find posts with any tags
        db.posts.find({tags: {$exists: true}})

Regular expressions:
         // posts where author starts with k
         db.posts.find({author: /^r*/i }) 

Counting: 
          // posts written by mike
	
  	
  db.posts.find({author:	
   roger }).count()	
  
Extending the Schema

    
        new_comment = {author: Bruce , 
                  date: new Date(),
                  text: Love Men at Work!!!! }

        new_info = { $push : {comments: new_comment},
                   $inc : {comments_count: 1}}

	
  db.posts.update({_id:	
   ... 	
  },	
  new_info)	
  
Extending the Schema

    
        { _id : ObjectId(4c4ba5c0672c685e5e8aabf3), 
          author : ”roger,
          date : Sat Jul 24 2010 19:47:11 GMT-0700 (PDT), 
          text : ”Down	
  Under...,
          tags : [ ”rockstar, ”men at work ],
          comments_count: 1, 
          comments : [
            
{
            
    
author : ”Bruce,
            
    
date : Sat Jul 24 2010 20:51:03 GMT-0700 (PDT),
            
    
text : ” Love Men at Work!!!!
            
}
          ]}
Extending the Schema

        // create index on nested documents:
        db.posts.ensureIndex({comments.author: 1})

        db.posts.find({comments.author:”Bruce”})

        // find last 5 posts:
        db.posts.find().sort({date:-1}).limit(5)

        // most commented post:
         db.posts.find().sort({comments_count:-1}).limit(1)

        When sorting, check if you need an index
Modeling Patterns

Single table inheritance

One to Many

Many to Many

Trees

Queues
Single Table Inheritance


    db.shapes.find()
     { _id: ObjectId(...), type: circle, area: 3.14, radius: 1}
     { _id: ObjectId(...), type: square, area: 4, d: 2}
     { _id: ObjectId(...), type: rect, area: 10, length: 5, width: 2}

    // find shapes where radius  0 
    db.shapes.find({radius: {$gt: 0}})

    // create index
    db.shapes.ensureIndex({radius: 1})
One to Many

- Embedded Array / Using Array Keys
    - slice operator to return subset of array
    - hard to find latest comments across all documents
One to Many

- Embedded Array / Array Keys
      - slice operator to return subset of array
      - hard to find latest comments across all documents

- Embedded tree
      - Single document
      - Natural
One to Many

- Embedded Array / Array Keys
      - slice operator to return subset of array
      - hard to find latest comments across all documents

- Embedded tree
      - Single document
      - Natural 
    
- Normalized (2 collections)
      - most flexible
      - more queries
Many - Many

Example:
  
- Product can be in many categories
- Category can have many products

  Products	

                      Category	

  - product_id	

                  - category_id	


            Prod_Categories	

            -  id	

            -  product_id	

            -  category_id
Many – Many
products:
 { _id: ObjectId(4c4ca23933fb5941681b912e),
   name: Sumatra Dark Roast,
   category_ids: [ ObjectId(4c4ca25433fb5941681b912f),
                   ObjectId(4c4ca25433fb5941681b92af”]}
Many – Many 
products:
    { _id: ObjectId(4c4ca23933fb5941681b912e),
      name: Sumatra Dark Roast,
      category_ids: [ ObjectId(4c4ca25433fb5941681b912f),
                      ObjectId(4c4ca25433fb5941681b92af”]}
    
categories:
    { _id: ObjectId(4c4ca25433fb5941681b912f), 
      name: Indonesia, 
      product_ids: [ ObjectId(4c4ca23933fb5941681b912e),
                     ObjectId(4c4ca30433fb5941681b9130),
                     ObjectId(4c4ca30433fb5941681b913a]}
Many - Many
products:
  { _id: ObjectId(4c4ca23933fb5941681b912e),
    name: Sumatra Dark Roast,
    category_ids: [ ObjectId(4c4ca25433fb5941681b912f),
                    ObjectId(4c4ca25433fb5941681b92af”]}
 
categories:
  { _id: ObjectId(4c4ca25433fb5941681b912f), 
    name: Indonesia, 
    product_ids: [ ObjectId(4c4ca23933fb5941681b912e),
                   ObjectId(4c4ca30433fb5941681b9130),
                   ObjectId(4c4ca30433fb5941681b913a]}

//All categories for a given product
db.categories.find({product_ids: ObjectId(4c4ca23933fb5941681b912e)})
Many - Many
products:
  { _id: ObjectId(4c4ca23933fb5941681b912e),
    name: Sumatra Dark Roast,
    category_ids: [ ObjectId(4c4ca25433fb5941681b912f),
                    ObjectId(4c4ca25433fb5941681b92af”]}
 
categories:
  { _id: ObjectId(4c4ca25433fb5941681b912f), 
    name: Indonesia, 
    product_ids: [ ObjectId(4c4ca23933fb5941681b912e),
                   ObjectId(4c4ca30433fb5941681b9130),
                   ObjectId(4c4ca30433fb5941681b913a]}

//All categories for a given product
db.categories.find({product_ids: ObjectId(4c4ca23933fb5941681b912e)})

//All products for a given category
db.products.find({category_ids: ObjectId(4c4ca25433fb5941681b912f)})
Alternative
products:
  { _id: ObjectId(4c4ca23933fb5941681b912e),
    name: Sumatra Dark Roast,
    category_ids: [ ObjectId(4c4ca25433fb5941681b912f),
                    ObjectId(4c4ca25433fb5941681b92af”]}
    
categories:
  { _id: ObjectId(4c4ca25433fb5941681b912f), 
    name: Indonesia}
Alternative
products:
  { _id: ObjectId(4c4ca23933fb5941681b912e),
    name: Sumatra Dark Roast,
    category_ids: [ ObjectId(4c4ca25433fb5941681b912f),
                    ObjectId(4c4ca25433fb5941681b92af”]}
    
categories:
  { _id: ObjectId(4c4ca25433fb5941681b912f), 
    name: Indonesia}

// All products for a given category
db.products.find({category_ids: ObjectId(4c4ca25433fb5941681b912f)})
Alternative
products:
  { _id: ObjectId(4c4ca23933fb5941681b912e),
    name: Sumatra Dark Roast,
    category_ids: [ ObjectId(4c4ca25433fb5941681b912f),
                    ObjectId(4c4ca25433fb5941681b92af”]}
    
categories:
  { _id: ObjectId(4c4ca25433fb5941681b912f), 
    name: Indonesia}

// All products for a given category
db.products.find({category_ids: ObjectId(4c4ca25433fb5941681b912f)}) 

// All categories for a given product
product = db.products.find(_id : some_id)
db.categories.find({_id : {$in : product.category_ids}})
Trees

Full Tree in Document

{ comments: [
     { author: rpb , text: ... , 
       replies: [
                   {author: Fred , text: ... ,
                    replies: []} 
       ]}
   ]}

        Pros: Single Document, Performance, Intuitive
        Cons: Hard to search, 16MB limit
Trees - continued

Parent Links
- Each node is stored as a document
- Contains the id of the parent

Child Links
- Each node contains the id s of the children
- Can support graphs (multiple parents / child)
Array of Ancestors
- Store Ancestors of a node 
    {   _id:   a }
    {   _id:   b, ancestors: [ a ], parent: a }
    {   _id:   c, ancestors: [ a, b ], parent: b }
    {   _id:   d, ancestors: [ a, b ], parent: b }
    {   _id:   e, ancestors: [ a ], parent: a }
    {   _id:   f, ancestors: [ a, e ], parent: e }
    {   _id:   g, ancestors: [ a, b, d ], parent: d }
Array of Ancestors
- Store Ancestors of a node 
    {   _id:   a }
    {   _id:   b, ancestors: [ a ], parent: a }
    {   _id:   c, ancestors: [ a, b ], parent: b }
    {   _id:   d, ancestors: [ a, b ], parent: b }
    {   _id:   e, ancestors: [ a ], parent: a }
    {   _id:   f, ancestors: [ a, e ], parent: e }
    {   _id:   g, ancestors: [ a, b, d ], parent: d }

//find all descendants of b:
db.tree2.find({ancestors: b })
Array of Ancestors
- Store Ancestors of a node 
 {   _id:   a }
 {   _id:   b, ancestors: [ a ], parent: a }
 {   _id:   c, ancestors: [ a, b ], parent: b }
 {   _id:   d, ancestors: [ a, b ], parent: b }
 {   _id:   e, ancestors: [ a ], parent: a }
 {   _id:   f, ancestors: [ a, e ], parent: e }
 {   _id:   g, ancestors: [ a, b, d ], parent: d }

//find all descendants of b:
db.tree2.find({ancestors: b })

//find all ancestors of f:
ancestors = db.tree2.findOne({_id: f }).ancestors
db.tree2.find({_id: { $in : ancestors})
Variable Keys
How to index ?
{ _id : uuid1,  	

    field1 : {   ctx1 : { ctx3 : 5, … },     	

                  ctx8 : { ctx3 : 5, … } }}	


db.MyCollection.find({ field1.ctx1.ctx3 : { $exists : true} })	


Rewrite:
{ _id : uuid1,  	

    field1 : {   key: ctx1 , value : { k:ctx3 , v : 5, … },     	

                  key: ctx8 , value : { k: ctx3 , v : 5, … } }}	

	

db.x.ensureIndex({ field1.key.k , 1})
findAndModify
Queue example

//Example: find highest priority job and mark

job = db.jobs.findAndModify({

          query: {inprogress: false},
          sort:   {priority: -1), 
          update: {$set: {inprogress: true, 
                          started: new Date()}},
          new: true})
Thanks !

More Related Content

What's hot

Indexing & Query Optimization
Indexing & Query OptimizationIndexing & Query Optimization
Indexing & Query Optimization
MongoDB
 
Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)
MongoSF
 
Database madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemyDatabase madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemy
Jaime Buelta
 
Optimizing Slow Queries with Indexes and Creativity
Optimizing Slow Queries with Indexes and CreativityOptimizing Slow Queries with Indexes and Creativity
Optimizing Slow Queries with Indexes and Creativity
MongoDB
 
Contando uma história com O.O.
Contando uma história com O.O.Contando uma história com O.O.
Contando uma história com O.O.
Vagner Zampieri
 

What's hot (19)

Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rapid and Scalable Development with MongoDB, PyMongo, and MingRapid and Scalable Development with MongoDB, PyMongo, and Ming
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
 
Indexing & Query Optimization
Indexing & Query OptimizationIndexing & Query Optimization
Indexing & Query Optimization
 
Schema Design (Mongo Austin)
Schema Design (Mongo Austin)Schema Design (Mongo Austin)
Schema Design (Mongo Austin)
 
Geospatial Indexing and Querying with MongoDB
Geospatial Indexing and Querying with MongoDBGeospatial Indexing and Querying with MongoDB
Geospatial Indexing and Querying with MongoDB
 
Reducing Development Time with MongoDB vs. SQL
Reducing Development Time with MongoDB vs. SQLReducing Development Time with MongoDB vs. SQL
Reducing Development Time with MongoDB vs. SQL
 
MongoDB and Indexes - MUG Denver - 20160329
MongoDB and Indexes - MUG Denver - 20160329MongoDB and Indexes - MUG Denver - 20160329
MongoDB and Indexes - MUG Denver - 20160329
 
Django - sql alchemy - jquery
Django - sql alchemy - jqueryDjango - sql alchemy - jquery
Django - sql alchemy - jquery
 
MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know
 
Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)
 
MongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB PerformanceMongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB Performance
 
Embedding a language into string interpolator
Embedding a language into string interpolatorEmbedding a language into string interpolator
Embedding a language into string interpolator
 
Database madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemyDatabase madness with_mongoengine_and_sql_alchemy
Database madness with_mongoengine_and_sql_alchemy
 
Optimizing Slow Queries with Indexes and Creativity
Optimizing Slow Queries with Indexes and CreativityOptimizing Slow Queries with Indexes and Creativity
Optimizing Slow Queries with Indexes and Creativity
 
CouchDB-Lucene
CouchDB-LuceneCouchDB-Lucene
CouchDB-Lucene
 
Contando uma história com O.O.
Contando uma história com O.O.Contando uma história com O.O.
Contando uma história com O.O.
 
An introduction into Spring Data
An introduction into Spring DataAn introduction into Spring Data
An introduction into Spring Data
 
Getting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETGetting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NET
 
Sequelize
SequelizeSequelize
Sequelize
 
Polyglot Persistence
Polyglot PersistencePolyglot Persistence
Polyglot Persistence
 

Viewers also liked

MongoDB. Области применения, преимущества и узкие места, тонкости использован...
MongoDB. Области применения, преимущества и узкие места, тонкости использован...MongoDB. Области применения, преимущества и узкие места, тонкости использован...
MongoDB. Области применения, преимущества и узкие места, тонкости использован...
phpdevby
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
Tyler Brock
 

Viewers also liked (10)

Building your first application w/mongoDB MongoSV2011
Building your first application w/mongoDB MongoSV2011Building your first application w/mongoDB MongoSV2011
Building your first application w/mongoDB MongoSV2011
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'tsThe Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
 
Кратко о MongoDB
Кратко о MongoDBКратко о MongoDB
Кратко о MongoDB
 
MongoDB and Schema Design
MongoDB and Schema DesignMongoDB and Schema Design
MongoDB and Schema Design
 
MongoDB. Области применения, преимущества и узкие места, тонкости использован...
MongoDB. Области применения, преимущества и узкие места, тонкости использован...MongoDB. Области применения, преимущества и узкие места, тонкости использован...
MongoDB. Области применения, преимущества и узкие места, тонкости использован...
 
Преимущества NoSQL баз данных на примере MongoDB
Преимущества NoSQL баз данных на примере MongoDBПреимущества NoSQL баз данных на примере MongoDB
Преимущества NoSQL баз данных на примере MongoDB
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
 
Выбор NoSQL базы данных для вашего проекта: "Не в свои сани не садись"
Выбор NoSQL базы данных для вашего проекта: "Не в свои сани не садись"Выбор NoSQL базы данных для вашего проекта: "Не в свои сани не садись"
Выбор NoSQL базы данных для вашего проекта: "Не в свои сани не садись"
 
Agg framework selectgroup feb2015 v2
Agg framework selectgroup feb2015 v2Agg framework selectgroup feb2015 v2
Agg framework selectgroup feb2015 v2
 
Webinar: 10-Step Guide to Creating a Single View of your Business
Webinar: 10-Step Guide to Creating a Single View of your BusinessWebinar: 10-Step Guide to Creating a Single View of your Business
Webinar: 10-Step Guide to Creating a Single View of your Business
 

Similar to Intro to MongoDB and datamodeling

10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling
DATAVERSITY
 
MongoDB (Advanced)
MongoDB (Advanced)MongoDB (Advanced)
MongoDB (Advanced)
TO THE NEW | Technology
 
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
Benefits of using MongoDB: Reduce Complexity & Adapt to ChangesBenefits of using MongoDB: Reduce Complexity & Adapt to Changes
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
Alex Nguyen
 

Similar to Intro to MongoDB and datamodeling (20)

Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDB
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev Teams
 
Starting with MongoDB
Starting with MongoDBStarting with MongoDB
Starting with MongoDB
 
MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)
 
MongoDB With Style
MongoDB With StyleMongoDB With Style
MongoDB With Style
 
Schema design
Schema designSchema design
Schema design
 
MongoDB (Advanced)
MongoDB (Advanced)MongoDB (Advanced)
MongoDB (Advanced)
 
Managing Social Content with MongoDB
Managing Social Content with MongoDBManaging Social Content with MongoDB
Managing Social Content with MongoDB
 
Full metal mongo
Full metal mongoFull metal mongo
Full metal mongo
 
Hands On Spring Data
Hands On Spring DataHands On Spring Data
Hands On Spring Data
 
Latinoware
LatinowareLatinoware
Latinoware
 
2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade
 
Mongodb intro
Mongodb introMongodb intro
Mongodb intro
 
Choosing a Shard key
Choosing a Shard keyChoosing a Shard key
Choosing a Shard key
 
Building Apps with MongoDB
Building Apps with MongoDBBuilding Apps with MongoDB
Building Apps with MongoDB
 
Building Your First MongoDB App
Building Your First MongoDB AppBuilding Your First MongoDB App
Building Your First MongoDB App
 
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial IndexesBack to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
 
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
Benefits of using MongoDB: Reduce Complexity & Adapt to ChangesBenefits of using MongoDB: Reduce Complexity & Adapt to Changes
Benefits of using MongoDB: Reduce Complexity & Adapt to Changes
 
MongoDB at GUL
MongoDB at GULMongoDB at GUL
MongoDB at GUL
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Intro to MongoDB and datamodeling

  • 1. Schema Design Roger Bodamer roger@analytica.com @rogerb
  • 2. A brief history of Data Modeling •  ISAM • COBOL •  Network •  Hiearchical •  Relational • 1970 E.F.Codd introduces 1st Normal Form (1NF) • 1971 E.F.Codd introduces 2nd and 3rd Normal Form (2NF, 3NF • 1974 Codd Boyce define Boyce/Codd Normal Form (BCNF) • 2002 Date, Darween, Lorentzos define 6th Normal Form (6NF) • Object
  • 3. So why model data?
  • 4. Modeling goals Goals: •  Avoid anomalies when inserting, updating or deleting •  Minimize redesign when extending the schema •  Make the model informative to users •  Avoid bias towards a particular style of query * source : wikipedia
  • 7. Some terms before we proceed RDBMS Document DBs Table Collection View / Row(s) JSON Document Index Index Join Embedding Linking across documents Partition Shard Partition Key Shard Key
  • 8. Recap Design documents that simply map to your application post  =  {author:   roger ,                  date:  new  Date(),                  text:   Down  Under... ,                  tags:  [ rockstar , men  at  work ]}
  • 9. Query operators Conditional operators: $ne, $in, $nin, $mod, $all, $size, $exists, $type, .. $lt, $lte, $gt, $gte, $ne, // find posts with any tags db.posts.find({tags: {$exists: true}})  
  • 10. Query operators Conditional operators: $ne, $in, $nin, $mod, $all, $size, $exists, $type, .. $lt, $lte, $gt, $gte, $ne, // find posts with any tags db.posts.find({tags: {$exists: true}}) Regular expressions: // posts where author starts with k db.posts.find({author: /^r*/i })  
  • 11. Query operators Conditional operators: $ne, $in, $nin, $mod, $all, $size, $exists, $type, .. $lt, $lte, $gt, $gte, $ne, // find posts with any tags db.posts.find({tags: {$exists: true}}) Regular expressions: // posts where author starts with k db.posts.find({author: /^r*/i }) Counting: // posts written by mike    db.posts.find({author:   roger }).count()  
  • 12. Extending the Schema new_comment = {author: Bruce , date: new Date(), text: Love Men at Work!!!! } new_info = { $push : {comments: new_comment}, $inc : {comments_count: 1}}  db.posts.update({_id:   ...  },  new_info)  
  • 13. Extending the Schema { _id : ObjectId(4c4ba5c0672c685e5e8aabf3), author : ”roger, date : Sat Jul 24 2010 19:47:11 GMT-0700 (PDT), text : ”Down  Under..., tags : [ ”rockstar, ”men at work ], comments_count: 1, comments : [ { author : ”Bruce, date : Sat Jul 24 2010 20:51:03 GMT-0700 (PDT), text : ” Love Men at Work!!!! } ]}
  • 14. Extending the Schema // create index on nested documents: db.posts.ensureIndex({comments.author: 1}) db.posts.find({comments.author:”Bruce”}) // find last 5 posts: db.posts.find().sort({date:-1}).limit(5) // most commented post: db.posts.find().sort({comments_count:-1}).limit(1) When sorting, check if you need an index
  • 15.
  • 16. Modeling Patterns Single table inheritance One to Many Many to Many Trees Queues
  • 17. Single Table Inheritance db.shapes.find() { _id: ObjectId(...), type: circle, area: 3.14, radius: 1} { _id: ObjectId(...), type: square, area: 4, d: 2} { _id: ObjectId(...), type: rect, area: 10, length: 5, width: 2} // find shapes where radius 0 db.shapes.find({radius: {$gt: 0}}) // create index db.shapes.ensureIndex({radius: 1})
  • 18. One to Many - Embedded Array / Using Array Keys - slice operator to return subset of array - hard to find latest comments across all documents
  • 19. One to Many - Embedded Array / Array Keys - slice operator to return subset of array - hard to find latest comments across all documents - Embedded tree - Single document - Natural
  • 20. One to Many - Embedded Array / Array Keys - slice operator to return subset of array - hard to find latest comments across all documents - Embedded tree - Single document - Natural - Normalized (2 collections) - most flexible - more queries
  • 21. Many - Many Example: - Product can be in many categories - Category can have many products Products Category - product_id - category_id Prod_Categories -  id -  product_id -  category_id
  • 22. Many – Many products: { _id: ObjectId(4c4ca23933fb5941681b912e), name: Sumatra Dark Roast, category_ids: [ ObjectId(4c4ca25433fb5941681b912f), ObjectId(4c4ca25433fb5941681b92af”]}
  • 23. Many – Many products: { _id: ObjectId(4c4ca23933fb5941681b912e), name: Sumatra Dark Roast, category_ids: [ ObjectId(4c4ca25433fb5941681b912f), ObjectId(4c4ca25433fb5941681b92af”]} categories: { _id: ObjectId(4c4ca25433fb5941681b912f), name: Indonesia, product_ids: [ ObjectId(4c4ca23933fb5941681b912e), ObjectId(4c4ca30433fb5941681b9130), ObjectId(4c4ca30433fb5941681b913a]}
  • 24. Many - Many products: { _id: ObjectId(4c4ca23933fb5941681b912e), name: Sumatra Dark Roast, category_ids: [ ObjectId(4c4ca25433fb5941681b912f), ObjectId(4c4ca25433fb5941681b92af”]} categories: { _id: ObjectId(4c4ca25433fb5941681b912f), name: Indonesia, product_ids: [ ObjectId(4c4ca23933fb5941681b912e), ObjectId(4c4ca30433fb5941681b9130), ObjectId(4c4ca30433fb5941681b913a]} //All categories for a given product db.categories.find({product_ids: ObjectId(4c4ca23933fb5941681b912e)})
  • 25. Many - Many products: { _id: ObjectId(4c4ca23933fb5941681b912e), name: Sumatra Dark Roast, category_ids: [ ObjectId(4c4ca25433fb5941681b912f), ObjectId(4c4ca25433fb5941681b92af”]} categories: { _id: ObjectId(4c4ca25433fb5941681b912f), name: Indonesia, product_ids: [ ObjectId(4c4ca23933fb5941681b912e), ObjectId(4c4ca30433fb5941681b9130), ObjectId(4c4ca30433fb5941681b913a]} //All categories for a given product db.categories.find({product_ids: ObjectId(4c4ca23933fb5941681b912e)}) //All products for a given category db.products.find({category_ids: ObjectId(4c4ca25433fb5941681b912f)})
  • 26. Alternative products: { _id: ObjectId(4c4ca23933fb5941681b912e), name: Sumatra Dark Roast, category_ids: [ ObjectId(4c4ca25433fb5941681b912f), ObjectId(4c4ca25433fb5941681b92af”]} categories: { _id: ObjectId(4c4ca25433fb5941681b912f), name: Indonesia}
  • 27. Alternative products: { _id: ObjectId(4c4ca23933fb5941681b912e), name: Sumatra Dark Roast, category_ids: [ ObjectId(4c4ca25433fb5941681b912f), ObjectId(4c4ca25433fb5941681b92af”]} categories: { _id: ObjectId(4c4ca25433fb5941681b912f), name: Indonesia} // All products for a given category db.products.find({category_ids: ObjectId(4c4ca25433fb5941681b912f)})
  • 28. Alternative products: { _id: ObjectId(4c4ca23933fb5941681b912e), name: Sumatra Dark Roast, category_ids: [ ObjectId(4c4ca25433fb5941681b912f), ObjectId(4c4ca25433fb5941681b92af”]} categories: { _id: ObjectId(4c4ca25433fb5941681b912f), name: Indonesia} // All products for a given category db.products.find({category_ids: ObjectId(4c4ca25433fb5941681b912f)}) // All categories for a given product product = db.products.find(_id : some_id) db.categories.find({_id : {$in : product.category_ids}})
  • 29. Trees Full Tree in Document { comments: [ { author: rpb , text: ... , replies: [ {author: Fred , text: ... , replies: []} ]} ]} Pros: Single Document, Performance, Intuitive Cons: Hard to search, 16MB limit
  • 30. Trees - continued Parent Links - Each node is stored as a document - Contains the id of the parent Child Links - Each node contains the id s of the children - Can support graphs (multiple parents / child)
  • 31. Array of Ancestors - Store Ancestors of a node { _id: a } { _id: b, ancestors: [ a ], parent: a } { _id: c, ancestors: [ a, b ], parent: b } { _id: d, ancestors: [ a, b ], parent: b } { _id: e, ancestors: [ a ], parent: a } { _id: f, ancestors: [ a, e ], parent: e } { _id: g, ancestors: [ a, b, d ], parent: d }
  • 32. Array of Ancestors - Store Ancestors of a node { _id: a } { _id: b, ancestors: [ a ], parent: a } { _id: c, ancestors: [ a, b ], parent: b } { _id: d, ancestors: [ a, b ], parent: b } { _id: e, ancestors: [ a ], parent: a } { _id: f, ancestors: [ a, e ], parent: e } { _id: g, ancestors: [ a, b, d ], parent: d } //find all descendants of b: db.tree2.find({ancestors: b })
  • 33. Array of Ancestors - Store Ancestors of a node { _id: a } { _id: b, ancestors: [ a ], parent: a } { _id: c, ancestors: [ a, b ], parent: b } { _id: d, ancestors: [ a, b ], parent: b } { _id: e, ancestors: [ a ], parent: a } { _id: f, ancestors: [ a, e ], parent: e } { _id: g, ancestors: [ a, b, d ], parent: d } //find all descendants of b: db.tree2.find({ancestors: b }) //find all ancestors of f: ancestors = db.tree2.findOne({_id: f }).ancestors db.tree2.find({_id: { $in : ancestors})
  • 34. Variable Keys How to index ? { _id : uuid1,   field1 : {   ctx1 : { ctx3 : 5, … },     ctx8 : { ctx3 : 5, … } }} db.MyCollection.find({ field1.ctx1.ctx3 : { $exists : true} }) Rewrite: { _id : uuid1,   field1 : {   key: ctx1 , value : { k:ctx3 , v : 5, … },     key: ctx8 , value : { k: ctx3 , v : 5, … } }} db.x.ensureIndex({ field1.key.k , 1})
  • 35. findAndModify Queue example //Example: find highest priority job and mark job = db.jobs.findAndModify({
 query: {inprogress: false}, sort: {priority: -1), update: {$set: {inprogress: true, started: new Date()}}, new: true})