SlideShare a Scribd company logo
Schema Design


by Alex Litvinok
Schema Design




   Basic unit of data – Document..
Schema Design

What is document?

• BSON Document
• Embedding
• Links across documents
Schema Design

Example

 01. event = {
 02.     _id:     ObjectId(‘47cc67093475061e3d95369d’),
 03.     name:    ‘MeetUP #2’,
 04.     date:    ISODate(‘2012-04-05 19:00:00'),
 05.     where:   {
 06.              city:     ‘Minsk’,
 07.              adress: ‘Nezavisimosti, 186’ }
 08. }
Schema Design

RDBMS? @#$.? NoSQL!
Relation DB     Document DB
Database        Database
Table           Collection
Row(s)          Document
Index           Index
Join            Embedding and Links
Partition       Shard
Partition Key   Shard Key
Schema Design

Why?


• Make queries easy and fast

• Facilitate sharding and automaticity
Schema Design

Strategy


• Start with a normalized model

• Embed docs for simplicity and optimization
Schema Design




     Normalized? Denormalized?
Schema Design

Normalized schema
01.   Order = {
02.       _id : orderId,        Order
03.       user : userInfo,      • _id
04.       items : [
                                • user
05.             productId1,     • items *
06.             productId2,
07.             productId3
08.       ]                     Product
09.   }                         •   _id
10.   Product = {               •   name
11.       _id: productId,       •   price
12.       name : name,          •   desc
13.       price : price,
14.       desc : description   * Link to collection of product
15.   }
Schema Design

Normalized schema


• Normalized documents are a perfectably
  acceptable way to use MongoDB.

• Normalized documents provide maximum
  flexibility.
Schema Design

Links across documents


DBRef
{ $ref : <collname>, $id : <idvalue>[, $db : <dbname>] }

Or simple storage of _id..
Schema Design

Denormalized schema
01.   Order = {
02.       _id : orderId,           Order
03.       user : userInfo,         • _id
04.       items : [ {
                                   • user
05.             _id: productId1,   • items
06.             name : name1,
07.             price : price1       • _id
                                     • name
08.       }, {
                                     • price
09.             _id: productId2,
10.             name : name2,        • _id
11.             price : price3       • name
12.       }]                         • price
13.   }
Schema Design

Denormalized schema

• Embedded documents are good for fast queries.

• The embedded documents always available with
  the parent documents.

• Embedded and nested documents are good for
  storing complex hierarchies.
Schema Design

Embedding documents
01.
      {
02.
          title : "Contributors",
03.
          data: [
04.
              { name: “Grover" },
05.
              { name: “James", surname: “Madison" },
06.
              { surname: “Grant" }
07.
          ]
08.
      }
09.
Schema Design




                ..fast queries
Schema Design

Indexes
Basics
> db.collection.ensureIndex({ name:1 });


Indexing on Embedded Fields
> db.collection.ensureIndex({ location.city:1 })


Compound Keys
> db.collection.ensureIndex({ name:1, age:-1 })
Schema Design

Also indexes..
The _id Index
•   Automatically created except capped collection
•   Index is special and cannot be deleted
•   Enforces uniqueness for its keys


Indexing Array Elements
•   Indexes for each element of the array


Compound Keys
•   Direction of the index ( 1 for ascending or -1 for descending )
Schema Design

Again indexes...

Create options
sparse, unique, dropDups, background, v…

Geospatial Indexing
> db.places.ensureIndex( { loc : "2d" } )
> db.places.ensureIndex( { loc : "2d" } , { min : -500 , max : 500 } )
> db.places.ensureIndex( { loc : "2d" } , { bits : 26 } )
Schema Design




       Analysis and Optimization
           Profiler | Explain
Schema Design

Database Profiler

Profiling Level
• 0 - Off
• 1 - log slow operations (by default, >100ms is considered slow)
• 2 - log all operations


> db.setProfilingLevel(2);
Schema Design

Database Profiler

Viewing the Data – collection system.profile
> db.system.profile.find()


 { "ts" : "Thu Jan 29 2009 15:19:32 GMT-0500 (EST)" , "info" : "query
test.$cmd ntoreturn:1 reslen:66 nscanned:0 <br>query: { profile: 2 }
nreturned:1 bytes:50" , "millis" : 0}
Schema Design

Explain
> db.collection.find( … ).explain()
{
    cursor : "BasicCursor",
    indexBounds : [ ],
    nscanned : 57594,
    nscannedObjects : 57594,
    nYields : 2 ,
    n:3,
    millis : 108,
    indexOnly : false,
    isMultiKey : false,
    nChunkSkips : 0
}
Schema Design




        From theory to Actions..
Schema Design

Seating plan
                {
                    _id: ObjectId,
                    event_id: ObjectId
                    seats: {
                       A1:1,
                       A2:1,
                       A3:0,
                       …
                       H30:0
                    }
                }
Schema Design

Seating plan

{
    _id: {
       event_id: ObjectId,
       seat: ‘C9’
    },
    updated: new Date(),
    state: ‘AVALIBLE’
}
Schema Design

Feed reader



                • Users
                • Feed
                • Entries
Schema Design

Feed reader

Storage users
{
  _id: ObjectId,
  name: ‘username’,
  feeds: [
        ObjectId,
        ObjectId,
        …
  ]
}
Schema Design

Feed reader
Storage feeds
{
   _id: ObjectId,
   url: ‘http://bbc.com/news/feed’,
   name: ‘BBC News’,
   latest: Date(‘2012-01-10T12:30:13Z’),
   enties:[{
        latest: Date(‘2012-01-10T12:30:13Z’),
        title: ‘Bomb kills Somali sport officials’,
        description: ‘…’, …
   }]
}
Schema Design

Some tips

1. Duplicate data for speed, reference data for integrity
2. Try to fetch data in a single query
3. Design documents to be self-sufficient
4. Override _id when you have your own simple, unique id
5. Don’t always use an index
Schema Design

Conclusion


•   Embedded docs are good for fast queries
•   Embedded and nested docs are good for storing hierarchies
•   Normalized docs are a most acceptable
Schema Design




                ?   ?   ?   ?

More Related Content

What's hot (19)

PPTX
Webinar: Back to Basics: Thinking in Documents
MongoDB
 
KEY
Schema Design by Example ~ MongoSF 2012
hungarianhc
 
PPTX
Building a Scalable Inbox System with MongoDB and Java
antoinegirbal
 
PPTX
MongoDB Advanced Schema Design - Inboxes
Jared Rosoff
 
PDF
Building your first app with mongo db
MongoDB
 
PPTX
Back to Basics Webinar 3: Schema Design Thinking in Documents
MongoDB
 
PDF
Agile Schema Design: An introduction to MongoDB
Stennie Steneker
 
PPTX
Back to Basics 1: Thinking in documents
MongoDB
 
PDF
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
Matias Cascallares
 
PDF
Building a Social Network with MongoDB
Fred Chu
 
PPTX
Socialite, the Open Source Status Feed
MongoDB
 
PPTX
Data Modeling for the Real World
Mike Friedman
 
PPTX
Socialite, the Open Source Status Feed Part 2: Managing the Social Graph
MongoDB
 
PPTX
Webinar: Data Modeling Examples in the Real World
MongoDB
 
PPTX
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
MongoDB
 
PPTX
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
MongoDB
 
PPT
Building Your First MongoDB App ~ Metadata Catalog
hungarianhc
 
PPTX
User Data Management with MongoDB
MongoDB
 
PPTX
Webinar: General Technical Overview of MongoDB for Dev Teams
MongoDB
 
Webinar: Back to Basics: Thinking in Documents
MongoDB
 
Schema Design by Example ~ MongoSF 2012
hungarianhc
 
Building a Scalable Inbox System with MongoDB and Java
antoinegirbal
 
MongoDB Advanced Schema Design - Inboxes
Jared Rosoff
 
Building your first app with mongo db
MongoDB
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
MongoDB
 
Agile Schema Design: An introduction to MongoDB
Stennie Steneker
 
Back to Basics 1: Thinking in documents
MongoDB
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
Matias Cascallares
 
Building a Social Network with MongoDB
Fred Chu
 
Socialite, the Open Source Status Feed
MongoDB
 
Data Modeling for the Real World
Mike Friedman
 
Socialite, the Open Source Status Feed Part 2: Managing the Social Graph
MongoDB
 
Webinar: Data Modeling Examples in the Real World
MongoDB
 
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
MongoDB
 
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
MongoDB
 
Building Your First MongoDB App ~ Metadata Catalog
hungarianhc
 
User Data Management with MongoDB
MongoDB
 
Webinar: General Technical Overview of MongoDB for Dev Teams
MongoDB
 

Similar to MongoDB Schema Design (20)

PPTX
Schema design mongo_boston
MongoDB
 
PPTX
Schema Design
MongoDB
 
PDF
MongoDB Schema Design Tips & Tricks
Juan Antonio Roy Couto
 
KEY
Modeling Data in MongoDB
lehresman
 
PDF
10gen Presents Schema Design and Data Modeling
DATAVERSITY
 
PPTX
Schema Design Best Practices with Buzz Moschetti
MongoDB
 
PDF
Semi Formal Model for Document Oriented Databases
Daniel Coupal
 
KEY
Schema Design (Mongo Austin)
MongoDB
 
PDF
MongoDB Schema Design (Richard Kreuter's Mongo Berlin preso)
MongoDB
 
KEY
Schema design
christkv
 
PPTX
Schema Design
MongoDB
 
PPTX
Jumpstart: Introduction to Schema Design
MongoDB
 
PDF
Schema & Design
MongoDB
 
PPTX
No SQL - MongoDB
Mirza Asif
 
KEY
Schema Design
MongoDB
 
PDF
Schema Design
MongoDB
 
PDF
MongoDB and Schema Design
Matias Cascallares
 
PDF
Intro to MongoDB and datamodeling
rogerbodamer
 
PDF
Schema Design
MongoDB
 
KEY
mongoDB at Visibiz
Mike Brocious
 
Schema design mongo_boston
MongoDB
 
Schema Design
MongoDB
 
MongoDB Schema Design Tips & Tricks
Juan Antonio Roy Couto
 
Modeling Data in MongoDB
lehresman
 
10gen Presents Schema Design and Data Modeling
DATAVERSITY
 
Schema Design Best Practices with Buzz Moschetti
MongoDB
 
Semi Formal Model for Document Oriented Databases
Daniel Coupal
 
Schema Design (Mongo Austin)
MongoDB
 
MongoDB Schema Design (Richard Kreuter's Mongo Berlin preso)
MongoDB
 
Schema design
christkv
 
Schema Design
MongoDB
 
Jumpstart: Introduction to Schema Design
MongoDB
 
Schema & Design
MongoDB
 
No SQL - MongoDB
Mirza Asif
 
Schema Design
MongoDB
 
Schema Design
MongoDB
 
MongoDB and Schema Design
Matias Cascallares
 
Intro to MongoDB and datamodeling
rogerbodamer
 
Schema Design
MongoDB
 
mongoDB at Visibiz
Mike Brocious
 
Ad

Recently uploaded (20)

PDF
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Lesson 2 - WATER,pH, BUFFERS, AND ACID-BASE.pdf
marvinnbustamante1
 
community health nursing question paper 2.pdf
Prince kumar
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Ad

MongoDB Schema Design

  • 2. Schema Design Basic unit of data – Document..
  • 3. Schema Design What is document? • BSON Document • Embedding • Links across documents
  • 4. Schema Design Example 01. event = { 02. _id: ObjectId(‘47cc67093475061e3d95369d’), 03. name: ‘MeetUP #2’, 04. date: ISODate(‘2012-04-05 19:00:00'), 05. where: { 06. city: ‘Minsk’, 07. adress: ‘Nezavisimosti, 186’ } 08. }
  • 5. Schema Design RDBMS? @#$.? NoSQL! Relation DB Document DB Database Database Table Collection Row(s) Document Index Index Join Embedding and Links Partition Shard Partition Key Shard Key
  • 6. Schema Design Why? • Make queries easy and fast • Facilitate sharding and automaticity
  • 7. Schema Design Strategy • Start with a normalized model • Embed docs for simplicity and optimization
  • 8. Schema Design Normalized? Denormalized?
  • 9. Schema Design Normalized schema 01. Order = { 02. _id : orderId, Order 03. user : userInfo, • _id 04. items : [ • user 05. productId1, • items * 06. productId2, 07. productId3 08. ] Product 09. } • _id 10. Product = { • name 11. _id: productId, • price 12. name : name, • desc 13. price : price, 14. desc : description * Link to collection of product 15. }
  • 10. Schema Design Normalized schema • Normalized documents are a perfectably acceptable way to use MongoDB. • Normalized documents provide maximum flexibility.
  • 11. Schema Design Links across documents DBRef { $ref : <collname>, $id : <idvalue>[, $db : <dbname>] } Or simple storage of _id..
  • 12. Schema Design Denormalized schema 01. Order = { 02. _id : orderId, Order 03. user : userInfo, • _id 04. items : [ { • user 05. _id: productId1, • items 06. name : name1, 07. price : price1 • _id • name 08. }, { • price 09. _id: productId2, 10. name : name2, • _id 11. price : price3 • name 12. }] • price 13. }
  • 13. Schema Design Denormalized schema • Embedded documents are good for fast queries. • The embedded documents always available with the parent documents. • Embedded and nested documents are good for storing complex hierarchies.
  • 14. Schema Design Embedding documents 01. { 02. title : "Contributors", 03. data: [ 04. { name: “Grover" }, 05. { name: “James", surname: “Madison" }, 06. { surname: “Grant" } 07. ] 08. } 09.
  • 15. Schema Design ..fast queries
  • 16. Schema Design Indexes Basics > db.collection.ensureIndex({ name:1 }); Indexing on Embedded Fields > db.collection.ensureIndex({ location.city:1 }) Compound Keys > db.collection.ensureIndex({ name:1, age:-1 })
  • 17. Schema Design Also indexes.. The _id Index • Automatically created except capped collection • Index is special and cannot be deleted • Enforces uniqueness for its keys Indexing Array Elements • Indexes for each element of the array Compound Keys • Direction of the index ( 1 for ascending or -1 for descending )
  • 18. Schema Design Again indexes... Create options sparse, unique, dropDups, background, v… Geospatial Indexing > db.places.ensureIndex( { loc : "2d" } ) > db.places.ensureIndex( { loc : "2d" } , { min : -500 , max : 500 } ) > db.places.ensureIndex( { loc : "2d" } , { bits : 26 } )
  • 19. Schema Design Analysis and Optimization Profiler | Explain
  • 20. Schema Design Database Profiler Profiling Level • 0 - Off • 1 - log slow operations (by default, >100ms is considered slow) • 2 - log all operations > db.setProfilingLevel(2);
  • 21. Schema Design Database Profiler Viewing the Data – collection system.profile > db.system.profile.find() { "ts" : "Thu Jan 29 2009 15:19:32 GMT-0500 (EST)" , "info" : "query test.$cmd ntoreturn:1 reslen:66 nscanned:0 <br>query: { profile: 2 } nreturned:1 bytes:50" , "millis" : 0}
  • 22. Schema Design Explain > db.collection.find( … ).explain() { cursor : "BasicCursor", indexBounds : [ ], nscanned : 57594, nscannedObjects : 57594, nYields : 2 , n:3, millis : 108, indexOnly : false, isMultiKey : false, nChunkSkips : 0 }
  • 23. Schema Design From theory to Actions..
  • 24. Schema Design Seating plan { _id: ObjectId, event_id: ObjectId seats: { A1:1, A2:1, A3:0, … H30:0 } }
  • 25. Schema Design Seating plan { _id: { event_id: ObjectId, seat: ‘C9’ }, updated: new Date(), state: ‘AVALIBLE’ }
  • 26. Schema Design Feed reader • Users • Feed • Entries
  • 27. Schema Design Feed reader Storage users { _id: ObjectId, name: ‘username’, feeds: [ ObjectId, ObjectId, … ] }
  • 28. Schema Design Feed reader Storage feeds { _id: ObjectId, url: ‘http://bbc.com/news/feed’, name: ‘BBC News’, latest: Date(‘2012-01-10T12:30:13Z’), enties:[{ latest: Date(‘2012-01-10T12:30:13Z’), title: ‘Bomb kills Somali sport officials’, description: ‘…’, … }] }
  • 29. Schema Design Some tips 1. Duplicate data for speed, reference data for integrity 2. Try to fetch data in a single query 3. Design documents to be self-sufficient 4. Override _id when you have your own simple, unique id 5. Don’t always use an index
  • 30. Schema Design Conclusion • Embedded docs are good for fast queries • Embedded and nested docs are good for storing hierarchies • Normalized docs are a most acceptable
  • 31. Schema Design ? ? ? ?