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

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 

Recently uploaded (20)

DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 

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