SlideShare a Scribd company logo
MYSQL TO
MONGO (FTW)
Thinking Differently About Schema Design
@AJSHARP

Alex Sharp
Lead Developer, OptimisCorp

alexjsharp.com

github.com/ajsharp
CVBEAST
Mongo has many cool
features such as schema-
free, aggregation w
map/reduce and many
others
                           Side project


interested more in
domain modeling more
than performance and
scaling benefits of
Mongo




EMBEDDED OBJECTS
CVBEAST

 App to represent people’s curriculum vitae, but not in an
                    academic sense
CVBEAST

 Focus on “micro” experiences not suitable for a typical CV
           (definitely not suitable for a resumé)
CVBEAST

Simple object model, centered around highlighting attributes
                        of a person
CVBEAST

   Started out building with MySQL, b/c it’s familiar
OBJECT MODEL
                 Person

                 has a

                   CV

                has many

            Experience

                 has many

          ...       ...     ...
OBJECT MODEL
                           Person

                           has a

                             CV

 links, tags, and         has many
  other arbitrary
    properties        Experience

                           has many

                    ...       ...     ...
RELATIONAL SCHEMA
         people
           - id
           - name
           - ...

         cvs
           - id
           - person_id
           - ...

         experiences
           - id
           - cv_id
           - ...
RELATIONAL SCHEMA


       Lots of pointless JOINs
RELATIONAL SCHEMA
         people
           - id
           - name
           - ...

         cvs
           - id
           - person_id
           - ...

         experiences
           - id
           - cv_id
           - ...
RELATIONAL SCHEMA
         people
           - id
           - name
           - ...

         cvs             tags
                           - id
           - id            - name
           - person_id     - ...
           - ...
                         links
         experiences       - id
                           - name
           - id            - ...
           - cv_id
           - ...
not b/c of premature
optimization




                       this bothers me
It misrepresents the object model
        at the storage layer
It also became difficult to work with
 despite my familiarity with MySQL
It makes sense to store object relationships in
     first-class entities/documents/tables
But true object properties should be
        stored as properties
Not relationships.
Especially when properties have no practical
        meaning without the parent
CAVEAT


                              This is not always the case.

                                                    ;-)
if you need to:
* store LOTS of embedded objects (i.e. papermill)
EXAMPLE
Experience Links
EXPERIENCE.LINKS = [...]

     Dead-simple collection of links attached to
                  an experience
RUBY MODEL CODE
 class Person
   include Mongoid::Document

   field :keywords, :type => Array

   index :keywords
   index 'cv.experiences.tags'
 end
class Experience
  include Mongoid::Document

  embeds_many :links
end

class Link
  include Mongoid::Document

  field :url
  field :title
  embedded_in :experience, :inverse_of => :links
end
class Experience
  include Mongoid::Document

  embeds_many :links
end                    Both models are embedded in the
                              person collection
class Link
  include Mongoid::Document

  field :url
  field :title
  embedded_in :experience, :inverse_of => :links
end
EXPERIENCE.LINKS = [...]

      A link is only relevant inside the
      context of an experience object.
EXPERIENCE.LINKS = [...]

      In other words, it is a property,
            not a relationship.
EXPERIENCE.LINKS = [...]

   Mongo brings the storage layer closer to
             the object model
EXPERIENCE.LINKS = [...]
{
    "title": "Presented at MongoLA",
    "links": [
      { "title": "Event Site",
        "url": "http://www.10gen.com/conferences/mongola2011"},
      { "title": "Slides",
        "url": "http://alexjsharp.com/posts/mongola-2010-slides"}
    ]
}
EXAMPLE
Embedded Search
MYSQL
An exercise in masochism
The difficulty in MySQL came in working with
            properties of a person
These properties are represented as tables
And tables must be joined
SIMPLE SEARCH QUERY
select * from people
  inner join cvs on cvs.person_id = people.id
  inner join experiences on experiences.cv_id = cvs.id
  inner join tags on tags.experience_id = experiences.id
  where tags.name = 'ruby';
INDEXES NEEDED
 -   tags.experience_id
 -   tags.name
 -   experiences.cv_id
 -   cvs.person_id
Seems extremely unnecessary for such a
         simple object model
MONGO
An exercise in ... not masochism
Embedded objects make this query easy.
MONGO DOCUMENT “SCHEMA”
{"name": "Alex Sharp",
   "cv": {
     "experiences": [
       { "title": "spoke at MongoLA",
          "date" : "Thu Jan 13 2011",
          "tags" : ["mongodb", "speaking"]
       },
       {"title": "..."}
     ]
   }
}
MONGO DOCUMENT “SCHEMA”
{"name": "Alex Sharp",
   "cv": {
     "experiences": [
       { "title": "spoke at MongoLA",
          "date" : "Thu Jan 13 2011",
          "tags" : ["mongodb", "speaking"]
       },
       {"title": "..."}
     ]
   }
}
   we want to search for these
RUBY MODEL CODE
 class Person
   include Mongoid::Document

   field :keywords, :type => Array

   index :keywords
   index 'cv.experiences.tags'
 end
RUBY MODEL CODE
class Cv
  include Mongoid::Document

  embeds_many :experiences
end

class Experience
  include Mongoid::Document

  field :tags,       :type => Array, :default => []

  embedded_in :cv, :inverse_of => :experiences
  embeds_many :links

  after_save lambda { |exp| exp.cv.person.update_keywords }
end
RUBY MODEL CODE
class Cv
  include Mongoid::Document
                                      simple property
  embeds_many :experiences
end                                  (not relationship)
class Experience
  include Mongoid::Document

  field :tags,       :type => Array, :default => []

  embedded_in :cv, :inverse_of => :experiences
  embeds_many :links

  after_save lambda { |exp| exp.cv.person.update_keywords }
end
RUBY SEARCH CODE
class Person

  # i.e. db.people.find({"cv.experiences.tags": "ruby"})
  def self.search_by_tag(term)
    collection.find('cv.experiences.tags' => 'ruby')
  end

end
COMPARISON
select * from people
  inner join cvs on cvs.person_id = people.id
  inner join experiences on experiences.cv_id = cvs.id
  inner join tags on tags.experience_id = experiences.id
  where tags.name = 'ruby';


                       vs
db.people.find('cv.experiences.tags' => 'ruby')
WINS: IMPEDENCE MIS-MATCH

   Object persistence format is closer to
         application usage format
WINS: F(X)-ALITY

    Plus, we don’t lose any functionality.
WINS: FAMILIARITY

  Indexing principles are extremely familiar to
           relational database users.
WINS: FAMILIARITY

          Only simpler ;)
WINS: SIMPLICITY

        Query syntax is simple.
WINS: SIMPLICITY

    Dot notation > JOIN semantics filth
SUMMARY

     Mongo is perfectly suited for
        an app like CVBeast.
SUMMARY

   Where a relational DB forces storing
    object properties as relationships
SUMMARY

 Mongo narrows this mismatch considerably
SUMMARY

      A CV is a document...
SUMMARY

   So a document-oriented datastore
          seems appropriate.
SUMMARY

    Many object models have this
        tree-like structure.
SUMMARY

 Be willing to step outside of your comfort zone
SUMMARY

   And use Mongo when it’s practical.

More Related Content

What's hot

An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
César Trigo
 
Top 10 frameworks of node js
Top 10 frameworks of node jsTop 10 frameworks of node js
Top 10 frameworks of node js
Habilelabs
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo Db
Alex Sharp
 
Mongodb basics and architecture
Mongodb basics and architectureMongodb basics and architecture
Mongodb basics and architecture
Bishal Khanal
 
MongoDB Strange Loop 2009
MongoDB Strange Loop 2009MongoDB Strange Loop 2009
MongoDB Strange Loop 2009
Mike Dirolf
 
MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)
Uwe Printz
 
Introduction to MongoDB (from Austin Code Camp)
Introduction to MongoDB (from Austin Code Camp)Introduction to MongoDB (from Austin Code Camp)
Introduction to MongoDB (from Austin Code Camp)
Chris Edwards
 
MongoDB Java Development - MongoBoston 2010
MongoDB Java Development - MongoBoston 2010MongoDB Java Development - MongoBoston 2010
MongoDB Java Development - MongoBoston 2010Eliot Horowitz
 
Mongo db workshop # 02
Mongo db workshop # 02Mongo db workshop # 02
Mongo db workshop # 02
FarhatParveen10
 
Building Apps with MongoDB
Building Apps with MongoDBBuilding Apps with MongoDB
Building Apps with MongoDB
Nate Abele
 
Mongo db operations_v2
Mongo db operations_v2Mongo db operations_v2
Mongo db operations_v2
Thanabalan Sathneeganandan
 
Mongo DB
Mongo DB Mongo DB
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented databaseWojciech Sznapka
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
Universidade de São Paulo
 
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
SpringPeople
 
Building your first app with mongo db
Building your first app with mongo dbBuilding your first app with mongo db
Building your first app with mongo db
MongoDB
 
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
MongoDB
 
MongoDB
MongoDBMongoDB
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema Design
MongoDB
 
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
Matias Cascallares
 

What's hot (20)

An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 
Top 10 frameworks of node js
Top 10 frameworks of node jsTop 10 frameworks of node js
Top 10 frameworks of node js
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo Db
 
Mongodb basics and architecture
Mongodb basics and architectureMongodb basics and architecture
Mongodb basics and architecture
 
MongoDB Strange Loop 2009
MongoDB Strange Loop 2009MongoDB Strange Loop 2009
MongoDB Strange Loop 2009
 
MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)
 
Introduction to MongoDB (from Austin Code Camp)
Introduction to MongoDB (from Austin Code Camp)Introduction to MongoDB (from Austin Code Camp)
Introduction to MongoDB (from Austin Code Camp)
 
MongoDB Java Development - MongoBoston 2010
MongoDB Java Development - MongoBoston 2010MongoDB Java Development - MongoBoston 2010
MongoDB Java Development - MongoBoston 2010
 
Mongo db workshop # 02
Mongo db workshop # 02Mongo db workshop # 02
Mongo db workshop # 02
 
Building Apps with MongoDB
Building Apps with MongoDBBuilding Apps with MongoDB
Building Apps with MongoDB
 
Mongo db operations_v2
Mongo db operations_v2Mongo db operations_v2
Mongo db operations_v2
 
Mongo DB
Mongo DB Mongo DB
Mongo DB
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented database
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
 
Building your first app with mongo db
Building your first app with mongo dbBuilding your first app with mongo db
Building your first app with mongo db
 
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
 
MongoDB
MongoDBMongoDB
MongoDB
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: 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
 

Similar to Mysql to mongo

X-TREME THEMES
X-TREME THEMESX-TREME THEMES
X-TREME THEMES
FITC
 
Word2vec in Postgres
Word2vec in PostgresWord2vec in Postgres
Word2vec in Postgres
Gary Sieling
 
OOScss Architecture For Rails Apps
OOScss Architecture For Rails AppsOOScss Architecture For Rails Apps
OOScss Architecture For Rails Apps
Netguru
 
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3
Salvatore Iaconesi
 
Semantic technologies in practice - KULeuven 2016
Semantic technologies in practice - KULeuven 2016Semantic technologies in practice - KULeuven 2016
Semantic technologies in practice - KULeuven 2016
Aad Versteden
 
Zend Framework And Doctrine
Zend Framework And DoctrineZend Framework And Doctrine
Zend Framework And Doctrine
isaaczfoster
 
Stop the noise! - Introduction to the JSON:API specification in Drupal
Stop the noise! - Introduction to the JSON:API specification in DrupalStop the noise! - Introduction to the JSON:API specification in Drupal
Stop the noise! - Introduction to the JSON:API specification in Drupal
Björn Brala
 
Using Mongoid with Ruby on Rails
Using Mongoid with Ruby on RailsUsing Mongoid with Ruby on Rails
Using Mongoid with Ruby on Rails
Nicholas Altobelli
 
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldEfficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
Marakana Inc.
 
Sass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LASass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LA
Jake Johnson
 
A Blink Into The Rails Magic
A Blink Into The Rails MagicA Blink Into The Rails Magic
A Blink Into The Rails Magic
Nikos Dimitrakopoulos
 
Ams adapters
Ams adaptersAms adapters
Ams adapters
Bruno Alló Bacarini
 
Getting into ember.js
Getting into ember.jsGetting into ember.js
Getting into ember.js
reybango
 
Keynote - Speaker: Grigori Melnik
Keynote - Speaker: Grigori Melnik Keynote - Speaker: Grigori Melnik
Keynote - Speaker: Grigori Melnik
MongoDB
 
MongoDB Days UK: Building Apps with the MEAN Stack
MongoDB Days UK: Building Apps with the MEAN StackMongoDB Days UK: Building Apps with the MEAN Stack
MongoDB Days UK: Building Apps with the MEAN Stack
MongoDB
 
Construction Techniques For Domain Specific Languages
Construction Techniques For Domain Specific LanguagesConstruction Techniques For Domain Specific Languages
Construction Techniques For Domain Specific LanguagesThoughtWorks
 
Rails Gems realize RESTful modeling patterns
Rails Gems realize RESTful modeling patternsRails Gems realize RESTful modeling patterns
Rails Gems realize RESTful modeling patterns
Toru Kawamura
 
Model Inheritance
Model InheritanceModel Inheritance
Model Inheritance
Loren Davie
 

Similar to Mysql to mongo (20)

X-TREME THEMES
X-TREME THEMESX-TREME THEMES
X-TREME THEMES
 
Word2vec in Postgres
Word2vec in PostgresWord2vec in Postgres
Word2vec in Postgres
 
OOScss Architecture For Rails Apps
OOScss Architecture For Rails AppsOOScss Architecture For Rails Apps
OOScss Architecture For Rails Apps
 
MongoDb and Windows Azure
MongoDb and Windows AzureMongoDb and Windows Azure
MongoDb and Windows Azure
 
The emerging world of mongo db csp
The emerging world of mongo db   cspThe emerging world of mongo db   csp
The emerging world of mongo db csp
 
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3
 
Semantic technologies in practice - KULeuven 2016
Semantic technologies in practice - KULeuven 2016Semantic technologies in practice - KULeuven 2016
Semantic technologies in practice - KULeuven 2016
 
Zend Framework And Doctrine
Zend Framework And DoctrineZend Framework And Doctrine
Zend Framework And Doctrine
 
Stop the noise! - Introduction to the JSON:API specification in Drupal
Stop the noise! - Introduction to the JSON:API specification in DrupalStop the noise! - Introduction to the JSON:API specification in Drupal
Stop the noise! - Introduction to the JSON:API specification in Drupal
 
Using Mongoid with Ruby on Rails
Using Mongoid with Ruby on RailsUsing Mongoid with Ruby on Rails
Using Mongoid with Ruby on Rails
 
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldEfficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
 
Sass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LASass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LA
 
A Blink Into The Rails Magic
A Blink Into The Rails MagicA Blink Into The Rails Magic
A Blink Into The Rails Magic
 
Ams adapters
Ams adaptersAms adapters
Ams adapters
 
Getting into ember.js
Getting into ember.jsGetting into ember.js
Getting into ember.js
 
Keynote - Speaker: Grigori Melnik
Keynote - Speaker: Grigori Melnik Keynote - Speaker: Grigori Melnik
Keynote - Speaker: Grigori Melnik
 
MongoDB Days UK: Building Apps with the MEAN Stack
MongoDB Days UK: Building Apps with the MEAN StackMongoDB Days UK: Building Apps with the MEAN Stack
MongoDB Days UK: Building Apps with the MEAN Stack
 
Construction Techniques For Domain Specific Languages
Construction Techniques For Domain Specific LanguagesConstruction Techniques For Domain Specific Languages
Construction Techniques For Domain Specific Languages
 
Rails Gems realize RESTful modeling patterns
Rails Gems realize RESTful modeling patternsRails Gems realize RESTful modeling patterns
Rails Gems realize RESTful modeling patterns
 
Model Inheritance
Model InheritanceModel Inheritance
Model Inheritance
 

More from Alex Sharp

Bldr: A Minimalist JSON Templating DSL
Bldr: A Minimalist JSON Templating DSLBldr: A Minimalist JSON Templating DSL
Bldr: A Minimalist JSON Templating DSL
Alex Sharp
 
Bldr - Rubyconf 2011 Lightning Talk
Bldr - Rubyconf 2011 Lightning TalkBldr - Rubyconf 2011 Lightning Talk
Bldr - Rubyconf 2011 Lightning Talk
Alex Sharp
 
Refactoring in Practice - Sunnyconf 2010
Refactoring in Practice - Sunnyconf 2010Refactoring in Practice - Sunnyconf 2010
Refactoring in Practice - Sunnyconf 2010Alex Sharp
 
Refactoring in Practice - Ruby Hoedown 2010
Refactoring in Practice - Ruby Hoedown 2010Refactoring in Practice - Ruby Hoedown 2010
Refactoring in Practice - Ruby Hoedown 2010Alex Sharp
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Alex Sharp
 
Practical Ruby Projects with MongoDB - Ruby Midwest
Practical Ruby Projects with MongoDB - Ruby MidwestPractical Ruby Projects with MongoDB - Ruby Midwest
Practical Ruby Projects with MongoDB - Ruby MidwestAlex Sharp
 
Practical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSFPractical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSF
Alex Sharp
 
Getting Comfortable with BDD
Getting Comfortable with BDDGetting Comfortable with BDD
Getting Comfortable with BDD
Alex Sharp
 
Testing Has Many Purposes
Testing Has Many PurposesTesting Has Many Purposes
Testing Has Many Purposes
Alex Sharp
 

More from Alex Sharp (9)

Bldr: A Minimalist JSON Templating DSL
Bldr: A Minimalist JSON Templating DSLBldr: A Minimalist JSON Templating DSL
Bldr: A Minimalist JSON Templating DSL
 
Bldr - Rubyconf 2011 Lightning Talk
Bldr - Rubyconf 2011 Lightning TalkBldr - Rubyconf 2011 Lightning Talk
Bldr - Rubyconf 2011 Lightning Talk
 
Refactoring in Practice - Sunnyconf 2010
Refactoring in Practice - Sunnyconf 2010Refactoring in Practice - Sunnyconf 2010
Refactoring in Practice - Sunnyconf 2010
 
Refactoring in Practice - Ruby Hoedown 2010
Refactoring in Practice - Ruby Hoedown 2010Refactoring in Practice - Ruby Hoedown 2010
Refactoring in Practice - Ruby Hoedown 2010
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
 
Practical Ruby Projects with MongoDB - Ruby Midwest
Practical Ruby Projects with MongoDB - Ruby MidwestPractical Ruby Projects with MongoDB - Ruby Midwest
Practical Ruby Projects with MongoDB - Ruby Midwest
 
Practical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSFPractical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSF
 
Getting Comfortable with BDD
Getting Comfortable with BDDGetting Comfortable with BDD
Getting Comfortable with BDD
 
Testing Has Many Purposes
Testing Has Many PurposesTesting Has Many Purposes
Testing Has Many Purposes
 

Recently uploaded

Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 

Recently uploaded (20)

Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 

Mysql to mongo

  • 1. MYSQL TO MONGO (FTW) Thinking Differently About Schema Design
  • 2. @AJSHARP Alex Sharp Lead Developer, OptimisCorp alexjsharp.com github.com/ajsharp
  • 3. CVBEAST Mongo has many cool features such as schema- free, aggregation w map/reduce and many others Side project interested more in domain modeling more than performance and scaling benefits of Mongo EMBEDDED OBJECTS
  • 4. CVBEAST App to represent people’s curriculum vitae, but not in an academic sense
  • 5. CVBEAST Focus on “micro” experiences not suitable for a typical CV (definitely not suitable for a resumé)
  • 6. CVBEAST Simple object model, centered around highlighting attributes of a person
  • 7. CVBEAST Started out building with MySQL, b/c it’s familiar
  • 8. OBJECT MODEL Person has a CV has many Experience has many ... ... ...
  • 9. OBJECT MODEL Person has a CV links, tags, and has many other arbitrary properties Experience has many ... ... ...
  • 10. RELATIONAL SCHEMA people - id - name - ... cvs - id - person_id - ... experiences - id - cv_id - ...
  • 11. RELATIONAL SCHEMA Lots of pointless JOINs
  • 12. RELATIONAL SCHEMA people - id - name - ... cvs - id - person_id - ... experiences - id - cv_id - ...
  • 13. RELATIONAL SCHEMA people - id - name - ... cvs tags - id - id - name - person_id - ... - ... links experiences - id - name - id - ... - cv_id - ...
  • 14. not b/c of premature optimization this bothers me
  • 15. It misrepresents the object model at the storage layer
  • 16. It also became difficult to work with despite my familiarity with MySQL
  • 17. It makes sense to store object relationships in first-class entities/documents/tables
  • 18. But true object properties should be stored as properties
  • 20. Especially when properties have no practical meaning without the parent
  • 21. CAVEAT This is not always the case. ;-) if you need to: * store LOTS of embedded objects (i.e. papermill)
  • 23. EXPERIENCE.LINKS = [...] Dead-simple collection of links attached to an experience
  • 24.
  • 25. RUBY MODEL CODE class Person include Mongoid::Document field :keywords, :type => Array index :keywords index 'cv.experiences.tags' end
  • 26. class Experience include Mongoid::Document embeds_many :links end class Link include Mongoid::Document field :url field :title embedded_in :experience, :inverse_of => :links end
  • 27. class Experience include Mongoid::Document embeds_many :links end Both models are embedded in the person collection class Link include Mongoid::Document field :url field :title embedded_in :experience, :inverse_of => :links end
  • 28. EXPERIENCE.LINKS = [...] A link is only relevant inside the context of an experience object.
  • 29. EXPERIENCE.LINKS = [...] In other words, it is a property, not a relationship.
  • 30. EXPERIENCE.LINKS = [...] Mongo brings the storage layer closer to the object model
  • 31. EXPERIENCE.LINKS = [...] { "title": "Presented at MongoLA", "links": [ { "title": "Event Site", "url": "http://www.10gen.com/conferences/mongola2011"}, { "title": "Slides", "url": "http://alexjsharp.com/posts/mongola-2010-slides"} ] }
  • 33.
  • 34. MYSQL An exercise in masochism
  • 35. The difficulty in MySQL came in working with properties of a person
  • 36. These properties are represented as tables
  • 37. And tables must be joined
  • 38. SIMPLE SEARCH QUERY select * from people inner join cvs on cvs.person_id = people.id inner join experiences on experiences.cv_id = cvs.id inner join tags on tags.experience_id = experiences.id where tags.name = 'ruby';
  • 39. INDEXES NEEDED - tags.experience_id - tags.name - experiences.cv_id - cvs.person_id
  • 40. Seems extremely unnecessary for such a simple object model
  • 41. MONGO An exercise in ... not masochism
  • 42. Embedded objects make this query easy.
  • 43. MONGO DOCUMENT “SCHEMA” {"name": "Alex Sharp", "cv": { "experiences": [ { "title": "spoke at MongoLA", "date" : "Thu Jan 13 2011", "tags" : ["mongodb", "speaking"] }, {"title": "..."} ] } }
  • 44. MONGO DOCUMENT “SCHEMA” {"name": "Alex Sharp", "cv": { "experiences": [ { "title": "spoke at MongoLA", "date" : "Thu Jan 13 2011", "tags" : ["mongodb", "speaking"] }, {"title": "..."} ] } } we want to search for these
  • 45. RUBY MODEL CODE class Person include Mongoid::Document field :keywords, :type => Array index :keywords index 'cv.experiences.tags' end
  • 46. RUBY MODEL CODE class Cv include Mongoid::Document embeds_many :experiences end class Experience include Mongoid::Document field :tags, :type => Array, :default => [] embedded_in :cv, :inverse_of => :experiences embeds_many :links after_save lambda { |exp| exp.cv.person.update_keywords } end
  • 47. RUBY MODEL CODE class Cv include Mongoid::Document simple property embeds_many :experiences end (not relationship) class Experience include Mongoid::Document field :tags, :type => Array, :default => [] embedded_in :cv, :inverse_of => :experiences embeds_many :links after_save lambda { |exp| exp.cv.person.update_keywords } end
  • 48. RUBY SEARCH CODE class Person # i.e. db.people.find({"cv.experiences.tags": "ruby"}) def self.search_by_tag(term) collection.find('cv.experiences.tags' => 'ruby') end end
  • 49. COMPARISON select * from people inner join cvs on cvs.person_id = people.id inner join experiences on experiences.cv_id = cvs.id inner join tags on tags.experience_id = experiences.id where tags.name = 'ruby'; vs db.people.find('cv.experiences.tags' => 'ruby')
  • 50. WINS: IMPEDENCE MIS-MATCH Object persistence format is closer to application usage format
  • 51. WINS: F(X)-ALITY Plus, we don’t lose any functionality.
  • 52. WINS: FAMILIARITY Indexing principles are extremely familiar to relational database users.
  • 53. WINS: FAMILIARITY Only simpler ;)
  • 54. WINS: SIMPLICITY Query syntax is simple.
  • 55. WINS: SIMPLICITY Dot notation > JOIN semantics filth
  • 56. SUMMARY Mongo is perfectly suited for an app like CVBeast.
  • 57. SUMMARY Where a relational DB forces storing object properties as relationships
  • 58. SUMMARY Mongo narrows this mismatch considerably
  • 59. SUMMARY A CV is a document...
  • 60. SUMMARY So a document-oriented datastore seems appropriate.
  • 61. SUMMARY Many object models have this tree-like structure.
  • 62. SUMMARY Be willing to step outside of your comfort zone
  • 63. SUMMARY And use Mongo when it’s practical.