SlideShare ist ein Scribd-Unternehmen logo
1 von 10
Downloaden Sie, um offline zu lesen
embedded documents?
             Pierre-Louis Gottfrois
             Bastien Murzeau
             Apéro Ruby Bordeaux, 6 décembre 2011



mardi 6 décembre 11                                 1
• Qu’est ce qu’un document embedded ?
                • Embedded or not ?
                • Recherche dans les embeddeds
                • Déclarations non standards




mardi 6 décembre 11                                     2
Qu’est-ce qu’un document
                      embedded ?
                • Un document embedded est un document
                      enregistré dans un autre
                      • dispose des mêmes caractéristiques qu’un document standard

                      • représente un Model

                • Un document embedded n’existe qu’au sein
                      de son document parent :
                      • toute interaction avec ce document doit se faire au travers de son
                         parent




mardi 6 décembre 11                                                                          3
Qu’est-ce qu’un document
                                    embedded ?
             class Conversation
               include Mongoid::Document

               belongs_to :author, :class_name => 'User'
               embeds_many :messages
             end

             class Message
               include Mongoid::Document

               belongs_to :author, :class_name => 'User'
               embedded_in :conversation
             end



              db.conversations.findOne()
             {
             	   "_id" : ObjectId("4e66465bf92ea16d8400000d"),
             	   "author_id" : ObjectId("4e25c0d6ff3f1f0e20000005"),
             	   "messages" : [
             	   	    {
             	   	    	   "_id" : ObjectId("4e66465bf92ea16d8400000e"),
             	   	    	   "author_id" : ObjectId("4e25c0d6ff3f1f0e20000005"),
             	   	    	   "body" : "message 1"
             	   	    },
             	   	    {
             	   	    	   "_id" : ObjectId("4e664a72f92ea16d9000005a"),
             	   	    	   "author_id" : ObjectId("4e25ec35ff3f1f0e230001a0"),
             	   	    	   "body" : "message 2"
             	   	    }
              }


mardi 6 décembre 11                                                             4
“Relate” what you occasionally need,
                   “Embed” what you always need.

             • Avantages :
                • Facile a manipuler
                • Jointure pre-construite pour gagner en
                      rapidite

             • Inconvenients :
                • Gros document (> 500k)
                • Limit de 16MB

mardi 6 décembre 11                                        5
Embedded or not ?
                • YES : stockage d’information en lien direct
                      avec le parent (tags, votes, bon de
                      commande, etc.)

                • YES : optimisation des performances
                      (duplication des données mais requete plus
                      rapide)

                • YES : stockage d’informations dites
                      “persistentes” (timeline, elements
                      supprimables, etc.)

mardi 6 décembre 11                                                6
// Tags
             link = {
                 title: "RubyBdx",
                 url: "http://rubybdx.org/"
                 tags: ['ruby', 'rails', 'bordeaux']
             }

             // Save this link to the links collection
             db.links.save(link)

             // Build an index on the array
             db.links.ensureIndex({tags : 1})

             // Queries like this use an index:
             db.links.find({tags : 'bordeaux'})




             // Votes
             {
             '_id': ObjectId('4b980a6dea2c3f4579da141e'),
             'voters': [
                 ObjectId('4b980a6dea2c3f4579da141f'),
                 ObjectId('4b980a6dea2c3f4579da143a'),
                 ObjectId('4b980a6dea2c3f4579da1429')
             ],
             'votes': 3
             }




mardi 6 décembre 11                                         7
Embedded or not ?
             • NO : Un document MongoDB ne peut pas
                  grossir de manière ‘non prévue’ :
                • Lors de la création d’une document, MongoDB alloue plus de mémoire
                      que la taille réellement occupée par le document => attention a ne pas
                      dépasser cette allocation

                • Une collection devrait toujours stocker des éléments de tailles
                      relativement similaires




mardi 6 décembre 11                                                                            8
Recherche dans les
                      documents embeddeds

             # Use the 'dot' notation on our conversation / message collections
             # Find all conversation that contains message from given author
             @conversations = Conversation.where("messages.author_id" => current_user.id)

             # More complexe queries with mongodb
             author_ids = [...]
             @conversations = Conversation.where("messages.author_id" => {'$in' =>
             author_ids})




mardi 6 décembre 11                                                                         9
Déclarations non standards
             • Un peu de polymorphisme
             • Pas de “embeded_in”

             class Notification
               embeds_one :object, :polymorphic => true
             end

             class Conversation
               include Mongoid::Document

               belongs_to :author, :class_name => 'User'
               embeds_many :messages
             end




mardi 6 décembre 11                                        10

Weitere ähnliche Inhalte

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Apéro Ruby Bdx - Embedded docs in MongoDB - 6/12/2011 -

  • 1. embedded documents? Pierre-Louis Gottfrois Bastien Murzeau Apéro Ruby Bordeaux, 6 décembre 2011 mardi 6 décembre 11 1
  • 2. • Qu’est ce qu’un document embedded ? • Embedded or not ? • Recherche dans les embeddeds • Déclarations non standards mardi 6 décembre 11 2
  • 3. Qu’est-ce qu’un document embedded ? • Un document embedded est un document enregistré dans un autre • dispose des mêmes caractéristiques qu’un document standard • représente un Model • Un document embedded n’existe qu’au sein de son document parent : • toute interaction avec ce document doit se faire au travers de son parent mardi 6 décembre 11 3
  • 4. Qu’est-ce qu’un document embedded ? class Conversation include Mongoid::Document belongs_to :author, :class_name => 'User' embeds_many :messages end class Message include Mongoid::Document belongs_to :author, :class_name => 'User' embedded_in :conversation end db.conversations.findOne() { "_id" : ObjectId("4e66465bf92ea16d8400000d"), "author_id" : ObjectId("4e25c0d6ff3f1f0e20000005"), "messages" : [ { "_id" : ObjectId("4e66465bf92ea16d8400000e"), "author_id" : ObjectId("4e25c0d6ff3f1f0e20000005"), "body" : "message 1" }, { "_id" : ObjectId("4e664a72f92ea16d9000005a"), "author_id" : ObjectId("4e25ec35ff3f1f0e230001a0"), "body" : "message 2" } } mardi 6 décembre 11 4
  • 5. “Relate” what you occasionally need, “Embed” what you always need. • Avantages : • Facile a manipuler • Jointure pre-construite pour gagner en rapidite • Inconvenients : • Gros document (> 500k) • Limit de 16MB mardi 6 décembre 11 5
  • 6. Embedded or not ? • YES : stockage d’information en lien direct avec le parent (tags, votes, bon de commande, etc.) • YES : optimisation des performances (duplication des données mais requete plus rapide) • YES : stockage d’informations dites “persistentes” (timeline, elements supprimables, etc.) mardi 6 décembre 11 6
  • 7. // Tags link = { title: "RubyBdx", url: "http://rubybdx.org/" tags: ['ruby', 'rails', 'bordeaux'] } // Save this link to the links collection db.links.save(link) // Build an index on the array db.links.ensureIndex({tags : 1}) // Queries like this use an index: db.links.find({tags : 'bordeaux'}) // Votes { '_id': ObjectId('4b980a6dea2c3f4579da141e'), 'voters': [ ObjectId('4b980a6dea2c3f4579da141f'), ObjectId('4b980a6dea2c3f4579da143a'), ObjectId('4b980a6dea2c3f4579da1429') ], 'votes': 3 } mardi 6 décembre 11 7
  • 8. Embedded or not ? • NO : Un document MongoDB ne peut pas grossir de manière ‘non prévue’ : • Lors de la création d’une document, MongoDB alloue plus de mémoire que la taille réellement occupée par le document => attention a ne pas dépasser cette allocation • Une collection devrait toujours stocker des éléments de tailles relativement similaires mardi 6 décembre 11 8
  • 9. Recherche dans les documents embeddeds # Use the 'dot' notation on our conversation / message collections # Find all conversation that contains message from given author @conversations = Conversation.where("messages.author_id" => current_user.id) # More complexe queries with mongodb author_ids = [...] @conversations = Conversation.where("messages.author_id" => {'$in' => author_ids}) mardi 6 décembre 11 9
  • 10. Déclarations non standards • Un peu de polymorphisme • Pas de “embeded_in” class Notification embeds_one :object, :polymorphic => true end class Conversation include Mongoid::Document belongs_to :author, :class_name => 'User' embeds_many :messages end mardi 6 décembre 11 10