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

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 MingRick Copeland
 
Indexing & Query Optimization
Indexing & Query OptimizationIndexing & Query Optimization
Indexing & Query OptimizationMongoDB
 
Schema Design (Mongo Austin)
Schema Design (Mongo Austin)Schema Design (Mongo Austin)
Schema Design (Mongo Austin)MongoDB
 
Geospatial Indexing and Querying with MongoDB
Geospatial Indexing and Querying with MongoDBGeospatial Indexing and Querying with MongoDB
Geospatial Indexing and Querying with MongoDBGrant Goodale
 
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. SQLMongoDB
 
MongoDB and Indexes - MUG Denver - 20160329
MongoDB and Indexes - MUG Denver - 20160329MongoDB and Indexes - MUG Denver - 20160329
MongoDB and Indexes - MUG Denver - 20160329Douglas Duncan
 
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 Norberto Leite
 
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
 
MongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB PerformanceMongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB Europe 2016 - Debugging MongoDB PerformanceMongoDB
 
Embedding a language into string interpolator
Embedding a language into string interpolatorEmbedding a language into string interpolator
Embedding a language into string interpolatorMichael Limansky
 
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_alchemyJaime 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 CreativityMongoDB
 
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
 
An introduction into Spring Data
An introduction into Spring DataAn introduction into Spring Data
An introduction into Spring DataOliver Gierke
 
Getting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETGetting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETTomas Jansson
 

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

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 MongoSV2011Steven Francia
 
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'tsMatias Cascallares
 
Кратко о MongoDB
Кратко о MongoDBКратко о MongoDB
Кратко о MongoDBGleb Lebedev
 
MongoDB. Области применения, преимущества и узкие места, тонкости использован...
MongoDB. Области применения, преимущества и узкие места, тонкости использован...MongoDB. Области применения, преимущества и узкие места, тонкости использован...
MongoDB. Области применения, преимущества и узкие места, тонкости использован...phpdevby
 
Преимущества NoSQL баз данных на примере MongoDB
Преимущества NoSQL баз данных на примере MongoDBПреимущества NoSQL баз данных на примере MongoDB
Преимущества NoSQL баз данных на примере MongoDBUNETA
 
MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation FrameworkTyler Brock
 
Выбор NoSQL базы данных для вашего проекта: "Не в свои сани не садись"
Выбор NoSQL базы данных для вашего проекта: "Не в свои сани не садись"Выбор NoSQL базы данных для вашего проекта: "Не в свои сани не садись"
Выбор NoSQL базы данных для вашего проекта: "Не в свои сани не садись"Alexey Zinoviev
 
Agg framework selectgroup feb2015 v2
Agg framework selectgroup feb2015 v2Agg framework selectgroup feb2015 v2
Agg framework selectgroup feb2015 v2MongoDB
 
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 BusinessMongoDB
 

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

Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDBrogerbodamer
 
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 ModelingDATAVERSITY
 
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 TeamsMongoDB
 
Starting with MongoDB
Starting with MongoDBStarting with MongoDB
Starting with MongoDBDoThinger
 
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)Uwe Printz
 
Schema design
Schema designSchema design
Schema designchristkv
 
Managing Social Content with MongoDB
Managing Social Content with MongoDBManaging Social Content with MongoDB
Managing Social Content with MongoDBMongoDB
 
Hands On Spring Data
Hands On Spring DataHands On Spring Data
Hands On Spring DataEric Bottard
 
2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL SpartakiadeJohannes Hoppe
 
Mongodb intro
Mongodb introMongodb intro
Mongodb introchristkv
 
Choosing a Shard key
Choosing a Shard keyChoosing a Shard key
Choosing a Shard keyMongoDB
 
Building Apps with MongoDB
Building Apps with MongoDBBuilding Apps with MongoDB
Building Apps with MongoDBNate Abele
 
Building Your First MongoDB App
Building Your First MongoDB AppBuilding Your First MongoDB App
Building Your First MongoDB AppHenrik Ingo
 
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 IndexesMongoDB
 
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 ChangesAlex 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

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
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 StreamsRoshan Dwivedi
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
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 Scriptwesley chun
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
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
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
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
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

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})