SlideShare une entreprise Scribd logo
1  sur  77
Télécharger pour lire hors ligne
Entity Relationships in
a Document Database
    MapReduce Views for SQL Users
Entity:
An object defined by its identity
and a thread of continuity[1]




             1. "Entity" Domain-driven Design Community <http://domaindrivendesign.org/node/109>.
Entity
Relationship
Model
Join vs. Collation
SQL Query Joining
Publishers and Books
SELECT
  `publisher`.`id`,
  `publisher`.`name`,
  `book`.`title`
FROM `publisher`
FULL OUTER JOIN `book`
  ON `publisher`.`id` = `book`.`publisher_id`
ORDER BY
  `publisher`.`id`,
  `book`.`title`;
Joined Result Set
publisher.id publisher.name          book.title
                              Building iPhone Apps with
  oreilly    O'Reilly Media
                              HTML, CSS, and JavaScript
                              CouchDB: The Definitive
  oreilly    O'Reilly Media
                                     Guide
                              DocBook: The Definitive
  oreilly    O'Reilly Media
                                     Guide

  oreilly    O'Reilly Media     RESTful Web Services
Joined Result Set
     Publisher (“left”)
publisher.id publisher.name          book.title
                              Building iPhone Apps with
  oreilly    O'Reilly Media
                              HTML, CSS, and JavaScript
                              CouchDB: The Definitive
  oreilly    O'Reilly Media
                                     Guide
                              DocBook: The Definitive
  oreilly    O'Reilly Media
                                     Guide

  oreilly    O'Reilly Media     RESTful Web Services
Joined Result Set
     Publisher (“left”)            Book “right”
publisher.id publisher.name          book.title
                              Building iPhone Apps with
  oreilly    O'Reilly Media
                              HTML, CSS, and JavaScript
                              CouchDB: The Definitive
  oreilly    O'Reilly Media
                                     Guide
                              DocBook: The Definitive
  oreilly    O'Reilly Media
                                     Guide

  oreilly    O'Reilly Media     RESTful Web Services
Collated Result Set
      key            id                 value

  ["oreilly",0]   "oreilly"        "O'Reilly Media"
                              "Building iPhone Apps with
  ["oreilly",1]   "oreilly"
                              HTML, CSS, and JavaScript"
                               "CouchDB: The Definitive
  ["oreilly",1]   "oreilly"
                                         Guide"
                               "DocBook: The Definitive
  ["oreilly",1]   "oreilly"
                                         Guide"
  ["oreilly",1]   "oreilly"    "RESTful Web Services"
Collated Result Set
    key            id                 value

["oreilly",0]   "oreilly"        "O'Reilly Media"        Publisher
                            "Building iPhone Apps with
["oreilly",1]   "oreilly"
                            HTML, CSS, and JavaScript"
                             "CouchDB: The Definitive
["oreilly",1]   "oreilly"
                                       Guide"
                             "DocBook: The Definitive
["oreilly",1]   "oreilly"
                                       Guide"
["oreilly",1]   "oreilly"    "RESTful Web Services"
Collated Result Set
    key            id                 value

["oreilly",0]   "oreilly"        "O'Reilly Media"        Publisher
                            "Building iPhone Apps with
["oreilly",1]   "oreilly"
                            HTML, CSS, and JavaScript"
                             "CouchDB: The Definitive
["oreilly",1]   "oreilly"
                                       Guide"
                                                          Books
                             "DocBook: The Definitive
["oreilly",1]   "oreilly"
                                       Guide"
["oreilly",1]   "oreilly"    "RESTful Web Services"
View Result Sets
Made up of columns and rows

Every row has the same three columns:
  • key
  • id
  • value
Columns can contain a mixture of logical data types
One to Many Relationships
Embedded Entities:
Nest related entities within a document
Embedded Entities
A single document represents the “one” entity

Nested entities (JSON Array) represents the “many” entities

Simplest way to create a one to many relationship
Example: Publisher
with Nested Books
{
  "_id":"oreilly",
  "collection":"publisher",
  "name":"O'Reilly Media",
  "books":[
    { "title":"CouchDB: The Definitive Guide" },
    { "title":"RESTful Web Services" },
    { "title":"DocBook: The Definitive Guide" },
    { "title":"Building iPhone Apps with HTML, CSS,
and JavaScript" }
  ]
}
Map Function
function(doc) {
  if ("publisher" == doc.collection) {
    emit([doc._id, 0], doc.name);
    for (var i in doc.books) {
      emit([doc._id, 1], doc.books[i].title);
    }
  }
}
Result Set
     key            id                 value

 ["oreilly",0]   "oreilly"        "O'Reilly Media"
                             "Building iPhone Apps with
 ["oreilly",1]   "oreilly"
                             HTML, CSS, and JavaScript"
                              "CouchDB: The Definitive
 ["oreilly",1]   "oreilly"
                                        Guide"
                              "DocBook: The Definitive
 ["oreilly",1]   "oreilly"
                                        Guide"
 ["oreilly",1]   "oreilly"    "RESTful Web Services"
Limitations
Only works if there aren’t a large number of related entities:
 • Too many nested entities can result in very large documents
 • Slow to transfer between client and server
 • Unwieldy to modify
 • Time-consuming to index
Related Documents:
Reference an entity by its identifier
Related Documents
A document representing the “one” entity

Separate documents for each “many” entity

Each “many” entity references its related
“one” entity by the “one” entity’s document identifier

Makes for smaller documents

Reduces the probability of document update conflicts
Example: Publisher
{
    "_id":"oreilly",
    "collection":"publisher",
    "name":"O'Reilly Media"
}
Example: Related Book
{
    "_id":"9780596155896",
    "collection":"book",
    "title":"CouchDB: The Definitive Guide",
    "publisher":"oreilly"
}
Map Function
function(doc) {
  if ("publisher" == doc.collection) {
    emit([doc._id, 0], doc.name);
  }
  if ("book" == doc.collection) {
    emit([doc.publisher, 1], doc.title);
  }
}
Result Set
      key                   id              value

["oreilly",0]   "oreilly"         "O'Reilly Media"
                                  "CouchDB: The Definitive
["oreilly",1]   "9780596155896"
                                  Guide"
["oreilly",1]   "9780596529260"   "RESTful Web Services"
                                  "Building iPhone Apps with
["oreilly",1]   "9780596805791"
                                  HTML, CSS, and JavaScript"
                                  "DocBook: The Definitive
["oreilly",1]   "9781565925809"
                                  Guide"
Limitations
When retrieving the entity on the “right” side of the relationship,
one cannot include any data from the entity on the “left” side of
the relationship without the use of an additional query

Only works for one to many relationships
Many to Many Relationships
List of Keys:
Reference entities by their identifiers
List of Keys
A document representing each “many” entity on the “left” side
of the relationship

Separate documents for each “many” entity on the “right” side
of the relationship

Each “many” entity on the “right” side of the relationship
maintains a list of document identifiers for its related “many”
entities on the “left” side of the relationship
Books and Related Authors
Example: Book
{
    "_id":"9780596805029",
    "collection":"book",
    "title":"DocBook 5: The Definitive Guide"
}
Example: Book
{
    "_id":"9781565920514",
    "collection":"book",
    "title":"Making TeX Work"
}
Example: Book
{
    "_id":"9781565925809",
    "collection":"book",
    "title":"DocBook: The Definitive Guide"
}
Example: Author
{
    "_id":"muellner",
    "collection":"author",
    "name":"Leonard Muellner",
    "books":[
      "9781565925809"
    ]
}
Example: Author
{
    "_id":"walsh",
    "collection":"author",
    "name":"Norman Walsh",
    "books":[
      "9780596805029",
      "9781565925809",
      "9781565920514"
    ]
}
Map Function
function(doc) {
  if ("book" == doc.collection) {
    emit([doc._id, 0], doc.title);
  }
  if ("author" == doc.collection) {
    for (var i in doc.books) {
      emit([doc.books[i], 1], doc.name);
    }
  }
}
Result Set
        key                   id                  value
["9780596805029",0] "9780596805029" "DocBook 5: The Definitive Guide"

["9780596805029",1] "walsh"          "Norman Walsh"

["9781565920514",0] "9781565920514" "Making TeX Work"

["9781565920514",1] "walsh"          "Norman Walsh"

["9781565925809",0] "9781565925809" "DocBook: The Definitive Guide"

["9781565925809",1] "muellner"       "Leonard Muellner"

["9781565925809",1] "walsh"          "Norman Walsh"
Authors and Related Books
Map Function
function(doc) {
  if ("author" == doc.collection) {
    emit([doc._id, 0], doc.name);
    for (var i in doc.books) {
      emit([doc._id, 1], {"_id":doc.books[i]});
    }
  }
}
Result Set
      key              id              value
["muellner",0]   "muellner"   "Leonard Muellner"

["muellner",1]   "muellner"   {"_id":"9781565925809"}

["walsh",0]      "walsh"      "Norman Walsh"

["walsh",1]      "walsh"      {"_id":"9780596805029"}

["walsh",1]      "walsh"      {"_id":"9781565920514"}

["walsh",1]      "walsh"      {"_id":"9781565925809"}
Including Docs
  include_docs=true
     key          id    value               doc (truncated)
["muellner",0] "muellner" …     {"name":"Leonard Muellner"}
["muellner",1] "muellner" …     {"title":"DocBook: The Definitive Guide"}
["walsh",0]   "walsh"   …       {"name":"Norman Walsh"}
["walsh",1]   "walsh"   …       {"title":"DocBook 5: The Definitive Guide"}
["walsh",1]   "walsh"   …       {"title":"Making TeX Work"}
["walsh",1]   "walsh"   …       {"title":"DocBook: The Definitive Guide"}
Or, we can reverse the references…
Example: Author
{
    "_id":"muellner",
    "collection":"author",
    "name":"Leonard Muellner"
}
Example: Author
{
    "_id":"walsh",
    "collection":"author",
    "name":"Norman Walsh"
}
Example: Book
{
    "_id":"9780596805029",
    "collection":"book",
    "title":"DocBook 5: The Definitive Guide",
    "authors":[
      "walsh"
    ]
}
Example: Book
{
    "_id":"9781565920514",
    "collection":"book",
    "title":"Making TeX Work",
    "authors":[
      "walsh"
    ]
}
Example: Book
{
    "_id":"9781565925809",
    "collection":"book",
    "title":"DocBook: The Definitive Guide",
    "authors":[
      "muellner",
      "walsh"
    ]
}
Map Function
function(doc) {
  if ("author" == doc.collection) {
    emit([doc._id, 0], doc.name);
  }
  if ("book" == doc.collection) {
    for (var i in doc.authors) {
      emit([doc.authors[i], 1], doc.title);
    }
  }
}
Result Set
     key                id                  value
["muellner",0] "muellner"     "Leonard Muellner"
["muellner",1] "9781565925809" "DocBook: The Definitive Guide"
["walsh",0]   "walsh"         "Norman Walsh"
["walsh",1]   "9780596805029" "DocBook 5: The Definitive Guide"
["walsh",1]   "9781565920514" "Making TeX Work"
["walsh",1]   "9781565925809" "DocBook: The Definitive Guide"
Limitations
Queries from the “right” side of the relationship cannot include
any data from entities on the “left” side of the relationship
(without the use of include_docs)

A document representing an entity with lots of relationships
could become quite large
Relationship Documents:
Create a document to represent each
individual relationship
Relationship Documents
A document representing each “many” entity on the “left” side
of the relationship

Separate documents for each “many” entity on the “right” side
of the relationship

Neither the “left” nor “right” side of the relationship contain any
direct references to each other

For each distinct relationship, a separate document includes the
document identifiers for both the “left” and “right” sides of the
relationship
Example: Book
{
    "_id":"9780596805029",
    "collection":"book",
    "title":"DocBook 5: The Definitive Guide"
}
Example: Book
{
    "_id":"9781565920514",
    "collection":"book",
    "title":"Making TeX Work"
}
Example: Book
{
    "_id":"9781565925809",
    "collection":"book",
    "title":"DocBook: The Definitive Guide"
}
Example: Author
{
    "_id":"muellner",
    "collection":"author",
    "name":"Leonard Muellner"
}
Example: Author
{
    "_id":"walsh",
    "collection":"author",
    "name":"Norman Walsh"
}
Example:
Relationship Document
{
    "_id":"44005f2c",
    "collection":"book-author",
    "book":"9780596805029",
    "author":"walsh"
}
Example:
Relationship Document
{
    "_id":"44005f72",
    "collection":"book-author",
    "book":"9781565920514",
    "author":"walsh"
}
Example:
Relationship Document
{
    "_id":"44006720",
    "collection":"book-author",
    "book":"9781565925809",
    "author":"muellner"
}
Example:
Relationship Document
{
    "_id":"44006b0d",
    "collection":"book-author",
    "book":"9781565925809",
    "author":"walsh"
}
Books and Related Authors
Map Function
function(doc) {
  if ("book" == doc.collection) {
    emit([doc._id, 0], doc.title);
  }
  if ("book-author" == doc.collection) {
    emit([doc.book, 1], {"_id":doc.author});
  }
}
Result Set
       key                 id                         value
["9780596805029",0] "9780596805029" "DocBook 5: The Definitive Guide"
["9780596805029",1] "44005f2c"      {"_id":"walsh"}
["9781565920514",0] "9781565920514" "Making TeX Work"
["9781565920514",1] "44005f72"      {"_id":"walsh"}
["9781565925809",0] "9781565925809" "DocBook: The Definitive Guide"
["9781565925809",1] "44006720"      {"_id":"muellner"}
["9781565925809",1] "44006b0d"      {"_id":"walsh"}
Including Docs
  include_docs=true
      key         id value               doc (truncated)
["9780596805029",0] … …      {"title":"DocBook 5: The Definitive Guide"}
["9780596805029",1] … …      {"name":"Norman Walsh"}
["9781565920514",0] … …      {"title":"Making TeX Work"}
["9781565920514",1] … …      {"author","name":"Norman Walsh"}
["9781565925809",0] … …      {"title":"DocBook: The Definitive Guide"}
["9781565925809",1] … …      {"name":"Leonard Muellner"}
["9781565925809",1] … …      {"name":"Norman Walsh"}
Authors and Related Books
Map Function
function(doc) {
  if ("author" == doc.collection) {
    emit([doc._id, 0], doc.name);
  }
  if ("book-author" == doc.collection) {
    emit([doc.author, 1], {"_id":doc.book});
  }
}
Result Set
      key              id              value
["muellner",0]   "muellner"   "Leonard Muellner"

["muellner",1]   "44006720"   {"_id":"9781565925809"}

["walsh",0]      "walsh"      "Norman Walsh"

["walsh",1]      "44005f2c"   {"_id":"9780596805029"}

["walsh",1]      "44005f72"   {"_id":"9781565920514"}

["walsh",1]      "44006b0d"   {"_id":"9781565925809"}
Including Docs
include_docs=true
     key       id value               doc (truncated)
["muellner",0] …   …      {"name":"Leonard Muellner"}
["muellner",1] …   …      {"title":"DocBook: The Definitive Guide"}
["walsh",0]   …    …      {"name":"Norman Walsh"}
["walsh",1]   …    …      {"title":"DocBook 5: The Definitive Guide"}
["walsh",1]   …    …      {"title":"Making TeX Work"}
["walsh",1]   …    …      {"title":"DocBook: The Definitive Guide"}
Limitations
Queries can only contain data from the “left” or “right” side of the
relationship (without the use of include_docs)

Maintaining relationship documents may require more work
Final Thoughts
Document Databases Compared
to Relational Databases
Document databases have no tables (and therefore no columns)

Indexes (views) are queried directly, instead of being used to
optimize more generalized queries

Result set columns can contain a mix of logical data types

No built-in concept of relationships between documents

Related entities can be embedded in a document, referenced from
a document, or both
Caveats
No referential integrity

No atomic transactions across document boundaries

Some patterns may involve denormalized (i.e. redundant) data

Data inconsistencies are inevitable (i.e. eventual consistency)

Consider the implications of replication—what may seem
consistent with one database may not be consistent across nodes
(e.g. referencing entities that don’t yet exist on the node)
Additional Techniques
Use the startkey and endkey parameters to retrieve one entity and
its related entities:
 startkey=["9781565925809"]&endkey=["9781565925809",{}]

Define a reduce function and use grouping levels

Use UUIDs rather than natural keys for better performance

Use the bulk document API when writing Relationship Documents

When using the List of Keys or Relationship Documents patterns,
denormalize data so that you can have data from the “right” and
“left” side of the relationship within your query results
Cheat Sheet
                  Embedded     Related                 Relationship
                                          List of Keys
                   Entities   Documents                Documents

 One to Many         ✓           ✓
Many to Many                                     ✓                       ✓
<= N* Relations      ✓                           ✓
> N* Relations                   ✓                                       ✓


                                             *   where N is a large number for your system
http://oreilly.com/catalog/9781449303129/   http://oreilly.com/catalog/9781449303433/
Thank You
                                  @BradleyHolt
                             http://bradley-holt.com
                           bradley.holt@foundline.com




Copyright © 2011-2012 Bradley Holt. All rights reserved.

Contenu connexe

Tendances

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
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMike Friedman
 
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
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema DesignMongoDB
 
Modeling Data in MongoDB
Modeling Data in MongoDBModeling Data in MongoDB
Modeling Data in MongoDBlehresman
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDBNorberto Leite
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbAlex Sharp
 
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 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...MongoDB
 
Building web applications with mongo db presentation
Building web applications with mongo db presentationBuilding web applications with mongo db presentation
Building web applications with mongo db presentationMurat Çakal
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patternsjoergreichert
 
Socialite, the Open Source Status Feed
Socialite, the Open Source Status FeedSocialite, the Open Source Status Feed
Socialite, the Open Source Status FeedMongoDB
 
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
 
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
 
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
 
Dev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesDev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesMongoDB
 
Couchdb List and Show Introduction
Couchdb List and Show IntroductionCouchdb List and Show Introduction
Couchdb List and Show IntroductionOliver Kurowski
 

Tendances (20)

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
 
MongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World ExamplesMongoDB Schema Design: Four Real-World Examples
MongoDB Schema Design: Four Real-World Examples
 
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
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
Modeling Data in MongoDB
Modeling Data in MongoDBModeling Data in MongoDB
Modeling Data in MongoDB
 
Mongo db operations_v2
Mongo db operations_v2Mongo db operations_v2
Mongo db operations_v2
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDB
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo Db
 
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 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
Conceptos básicos. Seminario web 4: Indexación avanzada, índices de texto y g...
 
Building web applications with mongo db presentation
Building web applications with mongo db presentationBuilding web applications with mongo db presentation
Building web applications with mongo db presentation
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patterns
 
MongoDB and hadoop
MongoDB and hadoopMongoDB and hadoop
MongoDB and hadoop
 
Socialite, the Open Source Status Feed
Socialite, the Open Source Status FeedSocialite, the Open Source Status Feed
Socialite, the Open Source Status Feed
 
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
 
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
 
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
 
Dev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesDev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best Practices
 
Couchdb List and Show Introduction
Couchdb List and Show IntroductionCouchdb List and Show Introduction
Couchdb List and Show Introduction
 

Similaire à Entity Relationships in a Document Database at CouchConf Boston

Schema Design
Schema DesignSchema Design
Schema DesignMongoDB
 
Schema design mongo_boston
Schema design mongo_bostonSchema design mongo_boston
Schema design mongo_bostonMongoDB
 
Introduction to CouchDB
Introduction to CouchDBIntroduction to CouchDB
Introduction to CouchDBBogdan Sabău
 
Schema Design
Schema DesignSchema Design
Schema DesignMongoDB
 
Schema & Design
Schema & DesignSchema & Design
Schema & DesignMongoDB
 
Modeling JSON data for NoSQL document databases
Modeling JSON data for NoSQL document databasesModeling JSON data for NoSQL document databases
Modeling JSON data for NoSQL document databasesRyan CrawCour
 
Cognitive Search: Announcing the smartest enterprise search engine, now with ...
Cognitive Search: Announcing the smartest enterprise search engine, now with ...Cognitive Search: Announcing the smartest enterprise search engine, now with ...
Cognitive Search: Announcing the smartest enterprise search engine, now with ...Microsoft Tech Community
 
Tapping into Scientific Data with Hadoop and Flink
Tapping into Scientific Data with Hadoop and FlinkTapping into Scientific Data with Hadoop and Flink
Tapping into Scientific Data with Hadoop and FlinkMichael Häusler
 
CouchDB Open Source Bridge
CouchDB Open Source BridgeCouchDB Open Source Bridge
CouchDB Open Source BridgeChris Anderson
 
NoSQL databases and managing big data
NoSQL databases and managing big dataNoSQL databases and managing big data
NoSQL databases and managing big dataSteven Francia
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Designaaronheckmann
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema DesignMongoDB
 
What do we want computers to do for us?
What do we want computers to do for us? What do we want computers to do for us?
What do we want computers to do for us? Andrea Volpini
 
Introducing Azure DocumentDB - NoSQL, No Problem
Introducing Azure DocumentDB - NoSQL, No ProblemIntroducing Azure DocumentDB - NoSQL, No Problem
Introducing Azure DocumentDB - NoSQL, No ProblemAndrew Liu
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBNeil Henegan
 
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...MongoDB
 

Similaire à Entity Relationships in a Document Database at CouchConf Boston (20)

Schema Design
Schema DesignSchema Design
Schema Design
 
Schema design mongo_boston
Schema design mongo_bostonSchema design mongo_boston
Schema design mongo_boston
 
Library hacks
Library hacksLibrary hacks
Library hacks
 
Introduction to CouchDB
Introduction to CouchDBIntroduction to CouchDB
Introduction to CouchDB
 
Schema Design
Schema DesignSchema Design
Schema Design
 
Schema & Design
Schema & DesignSchema & Design
Schema & Design
 
Modeling JSON data for NoSQL document databases
Modeling JSON data for NoSQL document databasesModeling JSON data for NoSQL document databases
Modeling JSON data for NoSQL document databases
 
1428393873 mhkx3 ln
1428393873 mhkx3 ln1428393873 mhkx3 ln
1428393873 mhkx3 ln
 
Couchdb Nosql
Couchdb NosqlCouchdb Nosql
Couchdb Nosql
 
Cognitive Search: Announcing the smartest enterprise search engine, now with ...
Cognitive Search: Announcing the smartest enterprise search engine, now with ...Cognitive Search: Announcing the smartest enterprise search engine, now with ...
Cognitive Search: Announcing the smartest enterprise search engine, now with ...
 
Tapping into Scientific Data with Hadoop and Flink
Tapping into Scientific Data with Hadoop and FlinkTapping into Scientific Data with Hadoop and Flink
Tapping into Scientific Data with Hadoop and Flink
 
CouchDB Open Source Bridge
CouchDB Open Source BridgeCouchDB Open Source Bridge
CouchDB Open Source Bridge
 
NoSQL databases and managing big data
NoSQL databases and managing big dataNoSQL databases and managing big data
NoSQL databases and managing big data
 
MongoDB Schema Design
MongoDB Schema DesignMongoDB Schema Design
MongoDB Schema Design
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema Design
 
What do we want computers to do for us?
What do we want computers to do for us? What do we want computers to do for us?
What do we want computers to do for us?
 
lecture_34e.pptx
lecture_34e.pptxlecture_34e.pptx
lecture_34e.pptx
 
Introducing Azure DocumentDB - NoSQL, No Problem
Introducing Azure DocumentDB - NoSQL, No ProblemIntroducing Azure DocumentDB - NoSQL, No Problem
Introducing Azure DocumentDB - NoSQL, No Problem
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
 

Plus de Bradley Holt

Domain-Driven Design at ZendCon 2012
Domain-Driven Design at ZendCon 2012Domain-Driven Design at ZendCon 2012
Domain-Driven Design at ZendCon 2012Bradley Holt
 
Domain-Driven Design
Domain-Driven DesignDomain-Driven Design
Domain-Driven DesignBradley Holt
 
CouchConf NYC CouchApps
CouchConf NYC CouchAppsCouchConf NYC CouchApps
CouchConf NYC CouchAppsBradley Holt
 
ZendCon 2011 UnCon Domain-Driven Design
ZendCon 2011 UnCon Domain-Driven DesignZendCon 2011 UnCon Domain-Driven Design
ZendCon 2011 UnCon Domain-Driven DesignBradley Holt
 
ZendCon 2011 Learning CouchDB
ZendCon 2011 Learning CouchDBZendCon 2011 Learning CouchDB
ZendCon 2011 Learning CouchDBBradley Holt
 
jQuery Conference Boston 2011 CouchApps
jQuery Conference Boston 2011 CouchAppsjQuery Conference Boston 2011 CouchApps
jQuery Conference Boston 2011 CouchAppsBradley Holt
 
OSCON 2011 Learning CouchDB
OSCON 2011 Learning CouchDBOSCON 2011 Learning CouchDB
OSCON 2011 Learning CouchDBBradley Holt
 
Load Balancing with Apache
Load Balancing with ApacheLoad Balancing with Apache
Load Balancing with ApacheBradley Holt
 
New Features in PHP 5.3
New Features in PHP 5.3New Features in PHP 5.3
New Features in PHP 5.3Bradley Holt
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPBradley Holt
 
Resource-Oriented Web Services
Resource-Oriented Web ServicesResource-Oriented Web Services
Resource-Oriented Web ServicesBradley Holt
 
Zend Framework Quick Start Walkthrough
Zend Framework Quick Start WalkthroughZend Framework Quick Start Walkthrough
Zend Framework Quick Start WalkthroughBradley Holt
 
Burlington, VT PHP Users Group Subversion Presentation
Burlington, VT PHP Users Group Subversion PresentationBurlington, VT PHP Users Group Subversion Presentation
Burlington, VT PHP Users Group Subversion PresentationBradley Holt
 

Plus de Bradley Holt (14)

Domain-Driven Design at ZendCon 2012
Domain-Driven Design at ZendCon 2012Domain-Driven Design at ZendCon 2012
Domain-Driven Design at ZendCon 2012
 
Domain-Driven Design
Domain-Driven DesignDomain-Driven Design
Domain-Driven Design
 
CouchConf NYC CouchApps
CouchConf NYC CouchAppsCouchConf NYC CouchApps
CouchConf NYC CouchApps
 
ZendCon 2011 UnCon Domain-Driven Design
ZendCon 2011 UnCon Domain-Driven DesignZendCon 2011 UnCon Domain-Driven Design
ZendCon 2011 UnCon Domain-Driven Design
 
ZendCon 2011 Learning CouchDB
ZendCon 2011 Learning CouchDBZendCon 2011 Learning CouchDB
ZendCon 2011 Learning CouchDB
 
jQuery Conference Boston 2011 CouchApps
jQuery Conference Boston 2011 CouchAppsjQuery Conference Boston 2011 CouchApps
jQuery Conference Boston 2011 CouchApps
 
OSCON 2011 Learning CouchDB
OSCON 2011 Learning CouchDBOSCON 2011 Learning CouchDB
OSCON 2011 Learning CouchDB
 
Load Balancing with Apache
Load Balancing with ApacheLoad Balancing with Apache
Load Balancing with Apache
 
Intermediate PHP
Intermediate PHPIntermediate PHP
Intermediate PHP
 
New Features in PHP 5.3
New Features in PHP 5.3New Features in PHP 5.3
New Features in PHP 5.3
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Resource-Oriented Web Services
Resource-Oriented Web ServicesResource-Oriented Web Services
Resource-Oriented Web Services
 
Zend Framework Quick Start Walkthrough
Zend Framework Quick Start WalkthroughZend Framework Quick Start Walkthrough
Zend Framework Quick Start Walkthrough
 
Burlington, VT PHP Users Group Subversion Presentation
Burlington, VT PHP Users Group Subversion PresentationBurlington, VT PHP Users Group Subversion Presentation
Burlington, VT PHP Users Group Subversion Presentation
 

Dernier

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
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
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
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
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
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
 
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
 
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
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
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
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 

Dernier (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
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
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
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
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
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!
 
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
 
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
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
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
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
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
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 

Entity Relationships in a Document Database at CouchConf Boston

  • 1. Entity Relationships in a Document Database MapReduce Views for SQL Users
  • 2. Entity: An object defined by its identity and a thread of continuity[1] 1. "Entity" Domain-driven Design Community <http://domaindrivendesign.org/node/109>.
  • 5. SQL Query Joining Publishers and Books SELECT `publisher`.`id`, `publisher`.`name`, `book`.`title` FROM `publisher` FULL OUTER JOIN `book` ON `publisher`.`id` = `book`.`publisher_id` ORDER BY `publisher`.`id`, `book`.`title`;
  • 6. Joined Result Set publisher.id publisher.name book.title Building iPhone Apps with oreilly O'Reilly Media HTML, CSS, and JavaScript CouchDB: The Definitive oreilly O'Reilly Media Guide DocBook: The Definitive oreilly O'Reilly Media Guide oreilly O'Reilly Media RESTful Web Services
  • 7. Joined Result Set Publisher (“left”) publisher.id publisher.name book.title Building iPhone Apps with oreilly O'Reilly Media HTML, CSS, and JavaScript CouchDB: The Definitive oreilly O'Reilly Media Guide DocBook: The Definitive oreilly O'Reilly Media Guide oreilly O'Reilly Media RESTful Web Services
  • 8. Joined Result Set Publisher (“left”) Book “right” publisher.id publisher.name book.title Building iPhone Apps with oreilly O'Reilly Media HTML, CSS, and JavaScript CouchDB: The Definitive oreilly O'Reilly Media Guide DocBook: The Definitive oreilly O'Reilly Media Guide oreilly O'Reilly Media RESTful Web Services
  • 9. Collated Result Set key id value ["oreilly",0] "oreilly" "O'Reilly Media" "Building iPhone Apps with ["oreilly",1] "oreilly" HTML, CSS, and JavaScript" "CouchDB: The Definitive ["oreilly",1] "oreilly" Guide" "DocBook: The Definitive ["oreilly",1] "oreilly" Guide" ["oreilly",1] "oreilly" "RESTful Web Services"
  • 10. Collated Result Set key id value ["oreilly",0] "oreilly" "O'Reilly Media" Publisher "Building iPhone Apps with ["oreilly",1] "oreilly" HTML, CSS, and JavaScript" "CouchDB: The Definitive ["oreilly",1] "oreilly" Guide" "DocBook: The Definitive ["oreilly",1] "oreilly" Guide" ["oreilly",1] "oreilly" "RESTful Web Services"
  • 11. Collated Result Set key id value ["oreilly",0] "oreilly" "O'Reilly Media" Publisher "Building iPhone Apps with ["oreilly",1] "oreilly" HTML, CSS, and JavaScript" "CouchDB: The Definitive ["oreilly",1] "oreilly" Guide" Books "DocBook: The Definitive ["oreilly",1] "oreilly" Guide" ["oreilly",1] "oreilly" "RESTful Web Services"
  • 12. View Result Sets Made up of columns and rows Every row has the same three columns: • key • id • value Columns can contain a mixture of logical data types
  • 13. One to Many Relationships
  • 14. Embedded Entities: Nest related entities within a document
  • 15. Embedded Entities A single document represents the “one” entity Nested entities (JSON Array) represents the “many” entities Simplest way to create a one to many relationship
  • 16. Example: Publisher with Nested Books { "_id":"oreilly", "collection":"publisher", "name":"O'Reilly Media", "books":[ { "title":"CouchDB: The Definitive Guide" }, { "title":"RESTful Web Services" }, { "title":"DocBook: The Definitive Guide" }, { "title":"Building iPhone Apps with HTML, CSS, and JavaScript" } ] }
  • 17. Map Function function(doc) { if ("publisher" == doc.collection) { emit([doc._id, 0], doc.name); for (var i in doc.books) { emit([doc._id, 1], doc.books[i].title); } } }
  • 18. Result Set key id value ["oreilly",0] "oreilly" "O'Reilly Media" "Building iPhone Apps with ["oreilly",1] "oreilly" HTML, CSS, and JavaScript" "CouchDB: The Definitive ["oreilly",1] "oreilly" Guide" "DocBook: The Definitive ["oreilly",1] "oreilly" Guide" ["oreilly",1] "oreilly" "RESTful Web Services"
  • 19. Limitations Only works if there aren’t a large number of related entities: • Too many nested entities can result in very large documents • Slow to transfer between client and server • Unwieldy to modify • Time-consuming to index
  • 20. Related Documents: Reference an entity by its identifier
  • 21. Related Documents A document representing the “one” entity Separate documents for each “many” entity Each “many” entity references its related “one” entity by the “one” entity’s document identifier Makes for smaller documents Reduces the probability of document update conflicts
  • 22. Example: Publisher { "_id":"oreilly", "collection":"publisher", "name":"O'Reilly Media" }
  • 23. Example: Related Book { "_id":"9780596155896", "collection":"book", "title":"CouchDB: The Definitive Guide", "publisher":"oreilly" }
  • 24. Map Function function(doc) { if ("publisher" == doc.collection) { emit([doc._id, 0], doc.name); } if ("book" == doc.collection) { emit([doc.publisher, 1], doc.title); } }
  • 25. Result Set key id value ["oreilly",0] "oreilly" "O'Reilly Media" "CouchDB: The Definitive ["oreilly",1] "9780596155896" Guide" ["oreilly",1] "9780596529260" "RESTful Web Services" "Building iPhone Apps with ["oreilly",1] "9780596805791" HTML, CSS, and JavaScript" "DocBook: The Definitive ["oreilly",1] "9781565925809" Guide"
  • 26. Limitations When retrieving the entity on the “right” side of the relationship, one cannot include any data from the entity on the “left” side of the relationship without the use of an additional query Only works for one to many relationships
  • 27. Many to Many Relationships
  • 28. List of Keys: Reference entities by their identifiers
  • 29. List of Keys A document representing each “many” entity on the “left” side of the relationship Separate documents for each “many” entity on the “right” side of the relationship Each “many” entity on the “right” side of the relationship maintains a list of document identifiers for its related “many” entities on the “left” side of the relationship
  • 30. Books and Related Authors
  • 31. Example: Book { "_id":"9780596805029", "collection":"book", "title":"DocBook 5: The Definitive Guide" }
  • 32. Example: Book { "_id":"9781565920514", "collection":"book", "title":"Making TeX Work" }
  • 33. Example: Book { "_id":"9781565925809", "collection":"book", "title":"DocBook: The Definitive Guide" }
  • 34. Example: Author { "_id":"muellner", "collection":"author", "name":"Leonard Muellner", "books":[ "9781565925809" ] }
  • 35. Example: Author { "_id":"walsh", "collection":"author", "name":"Norman Walsh", "books":[ "9780596805029", "9781565925809", "9781565920514" ] }
  • 36. Map Function function(doc) { if ("book" == doc.collection) { emit([doc._id, 0], doc.title); } if ("author" == doc.collection) { for (var i in doc.books) { emit([doc.books[i], 1], doc.name); } } }
  • 37. Result Set key id value ["9780596805029",0] "9780596805029" "DocBook 5: The Definitive Guide" ["9780596805029",1] "walsh" "Norman Walsh" ["9781565920514",0] "9781565920514" "Making TeX Work" ["9781565920514",1] "walsh" "Norman Walsh" ["9781565925809",0] "9781565925809" "DocBook: The Definitive Guide" ["9781565925809",1] "muellner" "Leonard Muellner" ["9781565925809",1] "walsh" "Norman Walsh"
  • 39. Map Function function(doc) { if ("author" == doc.collection) { emit([doc._id, 0], doc.name); for (var i in doc.books) { emit([doc._id, 1], {"_id":doc.books[i]}); } } }
  • 40. Result Set key id value ["muellner",0] "muellner" "Leonard Muellner" ["muellner",1] "muellner" {"_id":"9781565925809"} ["walsh",0] "walsh" "Norman Walsh" ["walsh",1] "walsh" {"_id":"9780596805029"} ["walsh",1] "walsh" {"_id":"9781565920514"} ["walsh",1] "walsh" {"_id":"9781565925809"}
  • 41. Including Docs include_docs=true key id value doc (truncated) ["muellner",0] "muellner" … {"name":"Leonard Muellner"} ["muellner",1] "muellner" … {"title":"DocBook: The Definitive Guide"} ["walsh",0] "walsh" … {"name":"Norman Walsh"} ["walsh",1] "walsh" … {"title":"DocBook 5: The Definitive Guide"} ["walsh",1] "walsh" … {"title":"Making TeX Work"} ["walsh",1] "walsh" … {"title":"DocBook: The Definitive Guide"}
  • 42. Or, we can reverse the references…
  • 43. Example: Author { "_id":"muellner", "collection":"author", "name":"Leonard Muellner" }
  • 44. Example: Author { "_id":"walsh", "collection":"author", "name":"Norman Walsh" }
  • 45. Example: Book { "_id":"9780596805029", "collection":"book", "title":"DocBook 5: The Definitive Guide", "authors":[ "walsh" ] }
  • 46. Example: Book { "_id":"9781565920514", "collection":"book", "title":"Making TeX Work", "authors":[ "walsh" ] }
  • 47. Example: Book { "_id":"9781565925809", "collection":"book", "title":"DocBook: The Definitive Guide", "authors":[ "muellner", "walsh" ] }
  • 48. Map Function function(doc) { if ("author" == doc.collection) { emit([doc._id, 0], doc.name); } if ("book" == doc.collection) { for (var i in doc.authors) { emit([doc.authors[i], 1], doc.title); } } }
  • 49. Result Set key id value ["muellner",0] "muellner" "Leonard Muellner" ["muellner",1] "9781565925809" "DocBook: The Definitive Guide" ["walsh",0] "walsh" "Norman Walsh" ["walsh",1] "9780596805029" "DocBook 5: The Definitive Guide" ["walsh",1] "9781565920514" "Making TeX Work" ["walsh",1] "9781565925809" "DocBook: The Definitive Guide"
  • 50. Limitations Queries from the “right” side of the relationship cannot include any data from entities on the “left” side of the relationship (without the use of include_docs) A document representing an entity with lots of relationships could become quite large
  • 51. Relationship Documents: Create a document to represent each individual relationship
  • 52. Relationship Documents A document representing each “many” entity on the “left” side of the relationship Separate documents for each “many” entity on the “right” side of the relationship Neither the “left” nor “right” side of the relationship contain any direct references to each other For each distinct relationship, a separate document includes the document identifiers for both the “left” and “right” sides of the relationship
  • 53. Example: Book { "_id":"9780596805029", "collection":"book", "title":"DocBook 5: The Definitive Guide" }
  • 54. Example: Book { "_id":"9781565920514", "collection":"book", "title":"Making TeX Work" }
  • 55. Example: Book { "_id":"9781565925809", "collection":"book", "title":"DocBook: The Definitive Guide" }
  • 56. Example: Author { "_id":"muellner", "collection":"author", "name":"Leonard Muellner" }
  • 57. Example: Author { "_id":"walsh", "collection":"author", "name":"Norman Walsh" }
  • 58. Example: Relationship Document { "_id":"44005f2c", "collection":"book-author", "book":"9780596805029", "author":"walsh" }
  • 59. Example: Relationship Document { "_id":"44005f72", "collection":"book-author", "book":"9781565920514", "author":"walsh" }
  • 60. Example: Relationship Document { "_id":"44006720", "collection":"book-author", "book":"9781565925809", "author":"muellner" }
  • 61. Example: Relationship Document { "_id":"44006b0d", "collection":"book-author", "book":"9781565925809", "author":"walsh" }
  • 62. Books and Related Authors
  • 63. Map Function function(doc) { if ("book" == doc.collection) { emit([doc._id, 0], doc.title); } if ("book-author" == doc.collection) { emit([doc.book, 1], {"_id":doc.author}); } }
  • 64. Result Set key id value ["9780596805029",0] "9780596805029" "DocBook 5: The Definitive Guide" ["9780596805029",1] "44005f2c" {"_id":"walsh"} ["9781565920514",0] "9781565920514" "Making TeX Work" ["9781565920514",1] "44005f72" {"_id":"walsh"} ["9781565925809",0] "9781565925809" "DocBook: The Definitive Guide" ["9781565925809",1] "44006720" {"_id":"muellner"} ["9781565925809",1] "44006b0d" {"_id":"walsh"}
  • 65. Including Docs include_docs=true key id value doc (truncated) ["9780596805029",0] … … {"title":"DocBook 5: The Definitive Guide"} ["9780596805029",1] … … {"name":"Norman Walsh"} ["9781565920514",0] … … {"title":"Making TeX Work"} ["9781565920514",1] … … {"author","name":"Norman Walsh"} ["9781565925809",0] … … {"title":"DocBook: The Definitive Guide"} ["9781565925809",1] … … {"name":"Leonard Muellner"} ["9781565925809",1] … … {"name":"Norman Walsh"}
  • 67. Map Function function(doc) { if ("author" == doc.collection) { emit([doc._id, 0], doc.name); } if ("book-author" == doc.collection) { emit([doc.author, 1], {"_id":doc.book}); } }
  • 68. Result Set key id value ["muellner",0] "muellner" "Leonard Muellner" ["muellner",1] "44006720" {"_id":"9781565925809"} ["walsh",0] "walsh" "Norman Walsh" ["walsh",1] "44005f2c" {"_id":"9780596805029"} ["walsh",1] "44005f72" {"_id":"9781565920514"} ["walsh",1] "44006b0d" {"_id":"9781565925809"}
  • 69. Including Docs include_docs=true key id value doc (truncated) ["muellner",0] … … {"name":"Leonard Muellner"} ["muellner",1] … … {"title":"DocBook: The Definitive Guide"} ["walsh",0] … … {"name":"Norman Walsh"} ["walsh",1] … … {"title":"DocBook 5: The Definitive Guide"} ["walsh",1] … … {"title":"Making TeX Work"} ["walsh",1] … … {"title":"DocBook: The Definitive Guide"}
  • 70. Limitations Queries can only contain data from the “left” or “right” side of the relationship (without the use of include_docs) Maintaining relationship documents may require more work
  • 72. Document Databases Compared to Relational Databases Document databases have no tables (and therefore no columns) Indexes (views) are queried directly, instead of being used to optimize more generalized queries Result set columns can contain a mix of logical data types No built-in concept of relationships between documents Related entities can be embedded in a document, referenced from a document, or both
  • 73. Caveats No referential integrity No atomic transactions across document boundaries Some patterns may involve denormalized (i.e. redundant) data Data inconsistencies are inevitable (i.e. eventual consistency) Consider the implications of replication—what may seem consistent with one database may not be consistent across nodes (e.g. referencing entities that don’t yet exist on the node)
  • 74. Additional Techniques Use the startkey and endkey parameters to retrieve one entity and its related entities: startkey=["9781565925809"]&endkey=["9781565925809",{}] Define a reduce function and use grouping levels Use UUIDs rather than natural keys for better performance Use the bulk document API when writing Relationship Documents When using the List of Keys or Relationship Documents patterns, denormalize data so that you can have data from the “right” and “left” side of the relationship within your query results
  • 75. Cheat Sheet Embedded Related Relationship List of Keys Entities Documents Documents One to Many ✓ ✓ Many to Many ✓ ✓ <= N* Relations ✓ ✓ > N* Relations ✓ ✓ * where N is a large number for your system
  • 76. http://oreilly.com/catalog/9781449303129/ http://oreilly.com/catalog/9781449303433/
  • 77. Thank You @BradleyHolt http://bradley-holt.com bradley.holt@foundline.com Copyright © 2011-2012 Bradley Holt. All rights reserved.

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. A full outer join effectively combines both left and right outer joins. If your relational database doesn&amp;#x2019;t support full outer joins then a left outer join is &amp;#x201C;close enough&amp;#x201D; for the following examples.\n
  6. Entities are joined together in a single row.\n
  7. Entities are joined together in a single row.\n
  8. Entities are joined together in a single row.\n
  9. Entities are collated together, but in separate rows.\nNote the use of compound keys.\n
  10. Entities are collated together, but in separate rows.\nNote the use of compound keys.\n
  11. Entities are collated together, but in separate rows.\nNote the use of compound keys.\n
  12. Result set may also include a doc column if include_docs is set to true.\n
  13. Result set may also include a doc column if include_docs is set to true.\n
  14. Result set may also include a doc column if include_docs is set to true.\n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. The &amp;#x201C;0&amp;#x201D; and &amp;#x201C;1&amp;#x201D; make publisher sort before the publisher&amp;#x2019;s books.\nNote the use of compound keys.\n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. Note that the keys are the same as with the embedded document approach, but the IDs are different.\n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. Note that the best we can do is emit the book IDs, as we don&amp;#x2019;t have access to any other book data.\n
  57. \n
  58. Note that it includes the doc having the referenced ID, not the doc from which the row was emitted.\nNote that the docs are truncated.\n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. Note that none of the entity documents contain any references to other entities.\n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. \n
  87. \n
  88. \n
  89. \n
  90. \n
  91. Note that the docs are truncated.\n
  92. \n
  93. \n
  94. \n
  95. Note that the docs are truncated.\n
  96. \n
  97. \n
  98. \n
  99. \n
  100. \n
  101. \n
  102. \n
  103. \n
  104. Note that these are trade-offs that provide associated benefits.\n
  105. Note that these are trade-offs that provide associated benefits.\n
  106. Note that these are trade-offs that provide associated benefits.\n
  107. Note that these are trade-offs that provide associated benefits.\n
  108. Note that these are trade-offs that provide associated benefits.\n
  109. Note that the startkey and endkey parameters need to be URL encoded.\nNote that one must account for the &amp;#x201C;left&amp;#x201D; entity when using grouping levels.\nNote that UUIDs are especially useful for Relationship Documents.\nNote that the bulk document API is not transactional!\n
  110. Note that the startkey and endkey parameters need to be URL encoded.\nNote that one must account for the &amp;#x201C;left&amp;#x201D; entity when using grouping levels.\nNote that UUIDs are especially useful for Relationship Documents.\nNote that the bulk document API is not transactional!\n
  111. Note that the startkey and endkey parameters need to be URL encoded.\nNote that one must account for the &amp;#x201C;left&amp;#x201D; entity when using grouping levels.\nNote that UUIDs are especially useful for Relationship Documents.\nNote that the bulk document API is not transactional!\n
  112. Note that the startkey and endkey parameters need to be URL encoded.\nNote that one must account for the &amp;#x201C;left&amp;#x201D; entity when using grouping levels.\nNote that UUIDs are especially useful for Relationship Documents.\nNote that the bulk document API is not transactional!\n
  113. Note that the startkey and endkey parameters need to be URL encoded.\nNote that one must account for the &amp;#x201C;left&amp;#x201D; entity when using grouping levels.\nNote that UUIDs are especially useful for Relationship Documents.\nNote that the bulk document API is not transactional!\n
  114. \n
  115. \n
  116. \n