SlideShare une entreprise Scribd logo
1  sur  40
Schema Design by Example
         Kevin Hanson
       Solutions Architect
         @hungarianhc
       kevin@10gen.com


                             1
Agenda


• MongoDB Data Model

• Blog Posts & Comments

• Geospatial Check-Ins

• Food For Thought
MongoDB Data Model:
  Rich Documents
 {
     title: ‘Who Needs Rows?’,
     reasons: [
       { name: ‘scalability’,
         desc: ‘no more joins!’ },
       { name: ‘human readable’,
         desc: ‘ah this is nice...’ }
     ],
     model: {
        relational: false,
        awesome: true
     }
 }
Embedded Documents
   { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
     author : "roger",
     date : "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)",
     text : "Spirited Away",
     tags : [ "Tezuka", "Manga" ],
     comments : [
	

     {
	

     	

     author : "Fred",
	

     	

     date : "Sat Jul 24 2010 20:51:03 GMT-0700 (PDT)",
	

     	

     text : "Best Movie Ever"
	

     }
     ]}
Parallels
Parallels

RDBMS               MongoDB
Table               Collection
Row                 Document
Column              Field
Index               Index
Join                Embedding &
                    Linking
Schema Object
Relational

                      Category
                  • Name
                  • Url




                           Article
       User       • Name
                                              Tag
• Name            • Slug             • Name
• Email Address   • Publish date     • Url
                  • Text




                     Comment
                  • Comment
                  • Date
                  • Author
MongoDB

                            Article
                     • Name
                     • Slug
                     • Publish date
     User            • Text
• Name               • Author
• Email Address
                         Comment[]
                      • Comment
                      • Date
                      • Author

                            Tag[]
                      • Value

                         Category[]
                      • Value
Blog Posts and Comments
How Should the Documents Look?

What Are We Going to Do with the
            Data?

       To embed or to link...
       That is the question!
1) Fully Embedded
{
    blog-title: ‘Commuting to Work’,
    blog-text: [
         ‘This section is about airplanes’,
         ‘this section is about trains’
    ],
    comments: [
      { author: ‘Kevin Hanson’,
        comment: ‘dude, what about driving?’ },
      { author: ‘John Smith’,
        comment: ‘this blog is aWful!!11!!!!’ }
    ],
}
1) Fully Embedded
1) Fully Embedded
Pros
• Can query the comments or the blog for results
• Cleanly encapsulated
1) Fully Embedded
Pros
• Can query the comments or the blog for results
• Cleanly encapsulated


Cons
• What if we get too many comments? (16MB
MongoDB doc size)
• What if we want our results to be comments, not
blog posts?
2) Separating Blog & Comments
{                             {
  _id:                          _id:
ObjectId("4c4ba5c0672c685     ObjectId("4c4ba5c0672c685e5e
e5e8aabf3")                   8aabf4")
  comment-ref:                  blog-ref:
ObjectId("4c4ba5c0672c685     ObjectId("4c4ba5c0672c685e5e
e5e8aabf4")                   8aabf3")
  blog-title: ‘Commuting to     comments: [
Work’,                            { author: ‘Kevin Hanson’,
  blog-text: [                      comment: ‘dude, what about
     ‘This section is about   driving?’ },
airplanes’,                       { author: ‘John Smith’,
     ‘this section is about         comment: ‘this blog is
trains’                       aWful!!11!!!!’ }
  ]                             ],
}                             }
2) Separating Blog & Comments
2) Separating Blog & Comments
Pros
• Blog Post Size Stays Constant
• Can Search Sets of Comments
2) Separating Blog & Comments
Pros
• Blog Post Size Stays Constant
• Can Search Sets of Comments


Cons
• Too Many Comments? (same problem)
• Managing Document Links
3) Each Comment Gets Own Doc
      {
          blog-title: ‘Commuting to Work’,
          blog-text: [
             ‘This section is about airplanes’,
             ‘this section is about trains’]
      }


      {
       commenter: ‘Kevin Hanson’,
       comment: ‘dude, what about driving?’
      }


      {
       commenter: ‘John Smith’,
       comment: ‘this blog is aWful!!11!!!!’
      }
3) Each Comment Gets Own Doc
3) Each Comment Gets Own Doc
Pros
• Can Query Individual Comments
• Never Need to Worry About Doc Size
3) Each Comment Gets Own Doc
Pros
• Can Query Individual Comments
• Never Need to Worry About Doc Size


Cons
• Many Documents
• Standard Use Cases Become Complicated
Managing Arrays
Pushing to an Array Infinitely...
• Document Will Grow Larger than Allocated
Space
• Document May Increase Max Doc Size of
16MB

Can this be avoided??
• Yes!
• A Hybrid of Linking and Embedding
Geospatial Check-Ins
We Need 3 Things
We Need 3 Things




Places    Check-Ins   Users
Places


                    Q: Current location
                       A: Places near
                          location




User Generated
   Content



                   Places
Inserting a Place


var p = { name: “10gen HQ”,
      address: “578 Broadway, 7th
Floor”,
      city: “New York”,
      zip: “10012”}

> db.places.save(p)
Tags, Geo Coordinates, and Tips


       { name: “10gen HQ”,
       address: “578 Broadway, 7th Floor”,
       city: “New York”,
       zip: “10012”,
       tags: [“MongoDB”, “business”],
       latlong: [“40.0”, “72.0”],
       tips: [{user: “kevin”, time:
“3/15/2012”,tip: “Make sure to stop by
for office hours!”}],}
Updating Tips


db.places.update({name:"10gen HQ"},

     {$push :{tips:

     
    
     {user:"nosh", time:
3/15/2012, 
 
        
    tip:"stop by for
office hours on 
     

     Wednesdays from 4-6"}}}}
Querying Places
• Creating Indexes
  ★db.places.ensureIndex({tags:1})
  ★db.places.ensureIndex({name:1})
  ★db.places.ensureIndex({latlong:”2d”}
   )

• Finding Places
  ★db.places.find({latlong:{$near:
    [40,70]}})

• Regular Expressions
  ★db.places.find({name: /
   ^typeaheadstring/)
User Check-Ins


Record User Check-Ins    Users




                 Stats           Stats



  Check-Ins              Users
Users
user1 = {

    name: “Kevin Hanson”

    e-mail: “kevin@10gen.com”,

    check-ins:
[4b97e62bf1d8c7152c9ccb74,
5a20e62bf1d8c736ab]
}

checkins [] = ObjectId reference to Check-
Ins Collection
Check-Ins
checkin = {

    place: “10gen HQ”,

    ts: 9/20/2010 10:12:00,

    userId: <object id of user>
}

Every Check-In is Two Operations
• Insert a Check-In Object (check-ins
collection)
• Update ($push) user object with check-in
Simple Stats
db.checkins.find({place: “10gen HQ”)



db.checkins.find({place: “10gen HQ”})
	

 	

  	

  	

  .sort({ts:-1}).limit(10)



db.checkins.find({place: “10gen HQ”, 	

	

 	

  	

  	

  ts: {$gt: midnight}}).count()
Stats w/ MapReduce
mapFunc = function() {emit(this.place, 1);}

reduceFunc = function(key, values) {return
Array.sum(values);}

res =
db.checkins.mapReduce(mapFunc,reduceFunc,

     {query: {timestamp: {$gt:nowminus3hrs}}})

res = [{_id:”10gen HQ”, value: 17}, ….., ….]
     ... or try using the new aggregation framework!
                   Chris Westin @ 1:50PM
Food For Thought
Data How the App Wants It
Think About How the Application Wants
the Data, Not How it is most “Normalized”

Example: Our Business Cards
More info at http://www.mongodb.org/

               Kevin Hanson
        Solutions Architect, 10gen
          twitter: @hungarianhc
              kevin@10gen.com



   Facebook      |      Twitter         |
http://bit.ly/   @mongodb   http://linkd.in/joinmongo
   LinkedIn
mongofb

Contenu connexe

Tendances

Dev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesDev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesMongoDB
 
Data Modeling for the Real World
Data Modeling for the Real WorldData Modeling for the Real World
Data Modeling for the Real WorldMike Friedman
 
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...MongoDB
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema DesignAlex Litvinok
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema DesignMongoDB
 
Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDBrogerbodamer
 
Back to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documentsBack to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documentsMongoDB
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsWebinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsMongoDB
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
 Back to Basics Webinar 3: Schema Design Thinking in Documents Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in DocumentsMongoDB
 
Building a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and JavaBuilding a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and Javaantoinegirbal
 
Socialite, the Open Source Status Feed
Socialite, the Open Source Status FeedSocialite, the Open Source Status Feed
Socialite, the Open Source Status FeedMongoDB
 
Learn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDBLearn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDBMarakana Inc.
 
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
Socialite, the Open Source Status Feed Part 3: Scaling the Data FeedSocialite, the Open Source Status Feed Part 3: Scaling the Data Feed
Socialite, the Open Source Status Feed Part 3: Scaling the Data FeedMongoDB
 
Schema design
Schema designSchema design
Schema designMongoDB
 
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
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosConceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosMongoDB
 
Building a Social Network with MongoDB
  Building a Social Network with MongoDB  Building a Social Network with MongoDB
Building a Social Network with MongoDBFred Chu
 
Socialite, the Open Source Status Feed Part 2: Managing the Social Graph
Socialite, the Open Source Status Feed Part 2: Managing the Social GraphSocialite, the Open Source Status Feed Part 2: Managing the Social Graph
Socialite, the Open Source Status Feed Part 2: Managing the Social GraphMongoDB
 
Schema Design
Schema DesignSchema Design
Schema DesignMongoDB
 

Tendances (19)

Dev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesDev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best Practices
 
Data Modeling for the Real World
Data Modeling for the Real WorldData Modeling for the Real World
Data Modeling for the Real World
 
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema Design
 
Schema Design with MongoDB
Schema Design with MongoDBSchema Design with MongoDB
Schema Design with MongoDB
 
Back to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documentsBack to Basics 1: Thinking in documents
Back to Basics 1: Thinking in documents
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsWebinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in Documents
 
Back to Basics Webinar 3: Schema Design Thinking in Documents
 Back to Basics Webinar 3: Schema Design Thinking in Documents Back to Basics Webinar 3: Schema Design Thinking in Documents
Back to Basics Webinar 3: Schema Design Thinking in Documents
 
Building a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and JavaBuilding a Scalable Inbox System with MongoDB and Java
Building a Scalable Inbox System with MongoDB and Java
 
Socialite, the Open Source Status Feed
Socialite, the Open Source Status FeedSocialite, the Open Source Status Feed
Socialite, the Open Source Status Feed
 
Learn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDBLearn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDB
 
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
Socialite, the Open Source Status Feed Part 3: Scaling the Data FeedSocialite, the Open Source Status Feed Part 3: Scaling the Data Feed
Socialite, the Open Source Status Feed Part 3: Scaling the Data Feed
 
Schema design
Schema designSchema design
Schema design
 
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
 
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentosConceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
Conceptos básicos. seminario web 3 : Diseño de esquema pensado para documentos
 
Building a Social Network with MongoDB
  Building a Social Network with MongoDB  Building a Social Network with MongoDB
Building a Social Network with MongoDB
 
Socialite, the Open Source Status Feed Part 2: Managing the Social Graph
Socialite, the Open Source Status Feed Part 2: Managing the Social GraphSocialite, the Open Source Status Feed Part 2: Managing the Social Graph
Socialite, the Open Source Status Feed Part 2: Managing the Social Graph
 
Schema Design
Schema DesignSchema Design
Schema Design
 

Similaire à Schema Design by Example ~ MongoSF 2012

MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...Prasoon Kumar
 
S01 e01 schema-design
S01 e01 schema-designS01 e01 schema-design
S01 e01 schema-designMongoDB
 
Mongo db eveningschemadesign
Mongo db eveningschemadesignMongo db eveningschemadesign
Mongo db eveningschemadesignMongoDB APAC
 
OSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialOSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialSteven Francia
 
MongoDB Strange Loop 2009
MongoDB Strange Loop 2009MongoDB Strange Loop 2009
MongoDB Strange Loop 2009Mike Dirolf
 
Managing Social Content with MongoDB
Managing Social Content with MongoDBManaging Social Content with MongoDB
Managing Social Content with MongoDBMongoDB
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011Steven Francia
 
MongoDB at RubyEnRails 2009
MongoDB at RubyEnRails 2009MongoDB at RubyEnRails 2009
MongoDB at RubyEnRails 2009Mike Dirolf
 
MongoDB NYC Python
MongoDB NYC PythonMongoDB NYC Python
MongoDB NYC PythonMike Dirolf
 
Webinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedWebinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedMongoDB
 
10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCup10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCupWebGeek Philippines
 
Mongodb intro
Mongodb introMongodb intro
Mongodb introchristkv
 
Back to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in DocumentsBack to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in DocumentsJoe Drumgoole
 
Marc s01 e02-crud-database
Marc s01 e02-crud-databaseMarc s01 e02-crud-database
Marc s01 e02-crud-databaseMongoDB
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...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 ModelingDATAVERSITY
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Railsrfischer20
 

Similaire à Schema Design by Example ~ MongoSF 2012 (20)

MongoDB at RuPy
MongoDB at RuPyMongoDB at RuPy
MongoDB at RuPy
 
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
 
S01 e01 schema-design
S01 e01 schema-designS01 e01 schema-design
S01 e01 schema-design
 
Mongo db eveningschemadesign
Mongo db eveningschemadesignMongo db eveningschemadesign
Mongo db eveningschemadesign
 
OSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialOSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB Tutorial
 
MongoDB Strange Loop 2009
MongoDB Strange Loop 2009MongoDB Strange Loop 2009
MongoDB Strange Loop 2009
 
Managing Social Content with MongoDB
Managing Social Content with MongoDBManaging Social Content with MongoDB
Managing Social Content with MongoDB
 
MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011MongoDB, PHP and the cloud - php cloud summit 2011
MongoDB, PHP and the cloud - php cloud summit 2011
 
MongoDB at RubyEnRails 2009
MongoDB at RubyEnRails 2009MongoDB at RubyEnRails 2009
MongoDB at RubyEnRails 2009
 
MongoDB NYC Python
MongoDB NYC PythonMongoDB NYC Python
MongoDB NYC Python
 
Webinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting StartedWebinar: Build an Application Series - Session 2 - Getting Started
Webinar: Build an Application Series - Session 2 - Getting Started
 
MongoDB for Genealogy
MongoDB for GenealogyMongoDB for Genealogy
MongoDB for Genealogy
 
10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCup10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCup
 
Mongodb intro
Mongodb introMongodb intro
Mongodb intro
 
Back to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in DocumentsBack to Basics Webinar 3 - Thinking in Documents
Back to Basics Webinar 3 - Thinking in Documents
 
Marc s01 e02-crud-database
Marc s01 e02-crud-databaseMarc s01 e02-crud-database
Marc s01 e02-crud-database
 
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
Webinarserie: Einführung in MongoDB: “Back to Basics” - Teil 3 - Interaktion ...
 
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
 
MongoDB
MongoDBMongoDB
MongoDB
 
MongoDB and Ruby on Rails
MongoDB and Ruby on RailsMongoDB and Ruby on Rails
MongoDB and Ruby on Rails
 

Dernier

The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
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
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
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
 
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
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
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
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: 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
 

Dernier (20)

The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
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
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
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
 
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
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
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
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: 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
 

Schema Design by Example ~ MongoSF 2012

  • 1. Schema Design by Example Kevin Hanson Solutions Architect @hungarianhc kevin@10gen.com 1
  • 2. Agenda • MongoDB Data Model • Blog Posts & Comments • Geospatial Check-Ins • Food For Thought
  • 3. MongoDB Data Model: Rich Documents { title: ‘Who Needs Rows?’, reasons: [ { name: ‘scalability’, desc: ‘no more joins!’ }, { name: ‘human readable’, desc: ‘ah this is nice...’ } ], model: { relational: false, awesome: true } }
  • 4. Embedded Documents { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "roger", date : "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)", text : "Spirited Away", tags : [ "Tezuka", "Manga" ], comments : [ { author : "Fred", date : "Sat Jul 24 2010 20:51:03 GMT-0700 (PDT)", text : "Best Movie Ever" } ]}
  • 6. Parallels RDBMS MongoDB Table Collection Row Document Column Field Index Index Join Embedding & Linking Schema Object
  • 7. Relational Category • Name • Url Article User • Name Tag • Name • Slug • Name • Email Address • Publish date • Url • Text Comment • Comment • Date • Author
  • 8. MongoDB Article • Name • Slug • Publish date User • Text • Name • Author • Email Address Comment[] • Comment • Date • Author Tag[] • Value Category[] • Value
  • 9. Blog Posts and Comments
  • 10.
  • 11. How Should the Documents Look? What Are We Going to Do with the Data? To embed or to link... That is the question!
  • 12. 1) Fully Embedded { blog-title: ‘Commuting to Work’, blog-text: [ ‘This section is about airplanes’, ‘this section is about trains’ ], comments: [ { author: ‘Kevin Hanson’, comment: ‘dude, what about driving?’ }, { author: ‘John Smith’, comment: ‘this blog is aWful!!11!!!!’ } ], }
  • 14. 1) Fully Embedded Pros • Can query the comments or the blog for results • Cleanly encapsulated
  • 15. 1) Fully Embedded Pros • Can query the comments or the blog for results • Cleanly encapsulated Cons • What if we get too many comments? (16MB MongoDB doc size) • What if we want our results to be comments, not blog posts?
  • 16. 2) Separating Blog & Comments { { _id: _id: ObjectId("4c4ba5c0672c685 ObjectId("4c4ba5c0672c685e5e e5e8aabf3") 8aabf4") comment-ref: blog-ref: ObjectId("4c4ba5c0672c685 ObjectId("4c4ba5c0672c685e5e e5e8aabf4") 8aabf3") blog-title: ‘Commuting to comments: [ Work’, { author: ‘Kevin Hanson’, blog-text: [ comment: ‘dude, what about ‘This section is about driving?’ }, airplanes’, { author: ‘John Smith’, ‘this section is about comment: ‘this blog is trains’ aWful!!11!!!!’ } ] ], } }
  • 17. 2) Separating Blog & Comments
  • 18. 2) Separating Blog & Comments Pros • Blog Post Size Stays Constant • Can Search Sets of Comments
  • 19. 2) Separating Blog & Comments Pros • Blog Post Size Stays Constant • Can Search Sets of Comments Cons • Too Many Comments? (same problem) • Managing Document Links
  • 20. 3) Each Comment Gets Own Doc { blog-title: ‘Commuting to Work’, blog-text: [ ‘This section is about airplanes’, ‘this section is about trains’] } { commenter: ‘Kevin Hanson’, comment: ‘dude, what about driving?’ } { commenter: ‘John Smith’, comment: ‘this blog is aWful!!11!!!!’ }
  • 21. 3) Each Comment Gets Own Doc
  • 22. 3) Each Comment Gets Own Doc Pros • Can Query Individual Comments • Never Need to Worry About Doc Size
  • 23. 3) Each Comment Gets Own Doc Pros • Can Query Individual Comments • Never Need to Worry About Doc Size Cons • Many Documents • Standard Use Cases Become Complicated
  • 24. Managing Arrays Pushing to an Array Infinitely... • Document Will Grow Larger than Allocated Space • Document May Increase Max Doc Size of 16MB Can this be avoided?? • Yes! • A Hybrid of Linking and Embedding
  • 26. We Need 3 Things
  • 27. We Need 3 Things Places Check-Ins Users
  • 28. Places Q: Current location A: Places near location User Generated Content Places
  • 29. Inserting a Place var p = { name: “10gen HQ”, address: “578 Broadway, 7th Floor”, city: “New York”, zip: “10012”} > db.places.save(p)
  • 30. Tags, Geo Coordinates, and Tips { name: “10gen HQ”, address: “578 Broadway, 7th Floor”, city: “New York”, zip: “10012”, tags: [“MongoDB”, “business”], latlong: [“40.0”, “72.0”], tips: [{user: “kevin”, time: “3/15/2012”,tip: “Make sure to stop by for office hours!”}],}
  • 31. Updating Tips db.places.update({name:"10gen HQ"}, {$push :{tips: {user:"nosh", time: 3/15/2012, tip:"stop by for office hours on Wednesdays from 4-6"}}}}
  • 32. Querying Places • Creating Indexes ★db.places.ensureIndex({tags:1}) ★db.places.ensureIndex({name:1}) ★db.places.ensureIndex({latlong:”2d”} ) • Finding Places ★db.places.find({latlong:{$near: [40,70]}}) • Regular Expressions ★db.places.find({name: / ^typeaheadstring/)
  • 33. User Check-Ins Record User Check-Ins Users Stats Stats Check-Ins Users
  • 34. Users user1 = { name: “Kevin Hanson” e-mail: “kevin@10gen.com”, check-ins: [4b97e62bf1d8c7152c9ccb74, 5a20e62bf1d8c736ab] } checkins [] = ObjectId reference to Check- Ins Collection
  • 35. Check-Ins checkin = { place: “10gen HQ”, ts: 9/20/2010 10:12:00, userId: <object id of user> } Every Check-In is Two Operations • Insert a Check-In Object (check-ins collection) • Update ($push) user object with check-in
  • 36. Simple Stats db.checkins.find({place: “10gen HQ”) db.checkins.find({place: “10gen HQ”}) .sort({ts:-1}).limit(10) db.checkins.find({place: “10gen HQ”, ts: {$gt: midnight}}).count()
  • 37. Stats w/ MapReduce mapFunc = function() {emit(this.place, 1);} reduceFunc = function(key, values) {return Array.sum(values);} res = db.checkins.mapReduce(mapFunc,reduceFunc, {query: {timestamp: {$gt:nowminus3hrs}}}) res = [{_id:”10gen HQ”, value: 17}, ….., ….] ... or try using the new aggregation framework! Chris Westin @ 1:50PM
  • 39. Data How the App Wants It Think About How the Application Wants the Data, Not How it is most “Normalized” Example: Our Business Cards
  • 40. More info at http://www.mongodb.org/ Kevin Hanson Solutions Architect, 10gen twitter: @hungarianhc kevin@10gen.com Facebook | Twitter | http://bit.ly/ @mongodb http://linkd.in/joinmongo LinkedIn mongofb

Notes de l'éditeur

  1. \n
  2. \n
  3. key-values... where values are constant, array of values, or document\n
  4. key-values... where values are constant, array of values, or document\n
  5. schema in an RDBMS is a THING. You must create a schema, and it is an object. In mongo, a schema is implied based on similar data structure, but there is NO schema...\n\nThe first rule of schema is that there is no schema.\n
  6. normal relational model - only reason to deviate is performance\n
  7. \n
  8. \n
  9. How should the documents look = what should the schema be?\n\nWhat are we going to do with our data?\n\n\n
  10. How should the documents look = what should the schema be?\n\nWhat are we going to do with our data?\n\n\n
  11. How should the documents look = what should the schema be?\n\nWhat are we going to do with our data?\n\n\n
  12. Mention that I have another talk\n
  13. Mention that I have another talk\n
  14. Mention that I have another talk\n
  15. Mention that I have another talk\n
  16. Mention that I have another talk\n
  17. Mention that I have another talk\n
  18. expensive to do the most obvious things... cheap to do the other operations....\n\n
  19. expensive to do the most obvious things... cheap to do the other operations....\n\n
  20. expensive to do the most obvious things... cheap to do the other operations....\n\n
  21. pushing to arrays... doc size growing...\n\ncomments / history...\n\npre-allocate... larger than the current doc. \n\nstrategy 2.5 - chunks of ten comments\n\ndownsides... investigate compaction / avoid\n
  22. \n
  23. key-values... where values are constant, array of values, or document\n
  24. key-values... where values are constant, array of values, or document\n
  25. key-values... where values are constant, array of values, or document\n
  26. key-values... where values are constant, array of values, or document\n
  27. key-values... where values are constant, array of values, or document\n
  28. key-values... where values are constant, array of values, or document\n
  29. key-values... where values are constant, array of values, or document\n
  30. key-values... where values are constant, array of values, or document\n
  31. key-values... where values are constant, array of values, or document\n
  32. key-values... where values are constant, array of values, or document\n
  33. key-values... where values are constant, array of values, or document\n
  34. key-values... where values are constant, array of values, or document\n
  35. key-values... where values are constant, array of values, or document\n
  36. \n
  37. key-values... where values are constant, array of values, or document\n
  38. \n