SlideShare a Scribd company logo
#MongoBoston


Building your first app:
an introduction to
MongoDB
Mike Friedman
Perl Engineer & Evangelist, 10gen
What is MongoDB?
MongoDB is a ___________
database
• Document
• Open source
• High performance
• Horizontally scalable
• Full featured
Document Database
• Not for .PDF & .DOC files
• A document is essentially an associative array
• Document == JSON Object
• Document == Perl Hash
• Document == Python Dict
• Document == Ruby Hash
• etc.
Open Source
• MongoDB is an open source project
• On GitHub
• Server licensed under AGPL
• Drivers licensed under Apache
• Started & sponsored by 10gen
• Commercial licenses available
• Contributions welcome
High Performance
• Written in C++
• Extensive use of memory-mapped files
 i.e. read-through write-through memory caching.
• Runs nearly everywhere
• Data serialized as BSON (fast parsing)
• Full support for primary & secondary indexes
• Document model = less work
Horizontally Scalable
Full Featured
• Ad Hoc queries
• Real time aggregation
• Rich query capabilities
• Geospatial features
• Support for most programming languages
• Flexible schema
http://www.mongodb.org/download
s
Mongo Shell
Document Database
RDBMS                MongoDB
Table, View   ➜   Collection
Row           ➜   Document
Index         ➜   Index
Join          ➜   Embedded Document
Foreign Key   ➜   Reference
Partition     ➜   Shard


Terminology
Typical (relational) ERD
MongoDB ERD
We will build a library
management application
        http://www.flickr.com/photos/somegeekintn/3484353131/
First step in any application is
Determine your entities
Library Management Application
Entities
• Library Patrons (users)
• Books (catalog)
• Authors
• Publishers
• Categories ??
In a relational based app
We would start by doing
schema design
Relational schema design
• Large ERD Diagrams
• Complex create table statements
• ORMs to map tables to objects
• Tables just to join tables together
• For this simple app we'd have 5 tables and 5 join
 tables
• Lots of revisions until we get it just right
In a MongoDB based app
We start building our
and let the schema evolve
app
MongoDB collections
• Users
• Books
• Authors
Working with MongoDB
Start with an object
(or array, hash, dict, etc)

user = {
              username: 'fred.jones',
              first_name: 'Fred',
              last_name: 'Jones',
}
Insert the record

> db.users.insert(user)




                  No collection creation
                  needed
Querying for the user
> db.users.findOne()
{
    "_id" : ObjectId("50804d0bd94ccab2da652599"),
    "username" : "fred.jones",
    "first_name" : "Fred",
    "last_name" : "Jones"
}
_id
• _id is the primary key in MongoDB
• Automatically indexed
• Automatically created as an ObjectId if not
 provided
• Any unique immutable value could be used
ObjectId
• ObjectId is a special 12 byte value
• Guaranteed to be unique across your cluster
• ObjectId("50804d0bd94ccab2da652599")


             Timestamp machine   PID Increment
Creating an author
> db.author.insert({
                       first_name: ’J.R.R.',
                       last_name: ‘Tolkien',
         bio: 'J.R.R. Tolkien (1892-1973), beloved throughout the
world as the creator of The Hobbit and The Lord of the Rings, was a
professor of Anglo-Saxon at Oxford, a fellow of Pembroke
College, and a fellow of Merton College until his retirement in 1959.
His chief interest was the linguistic aspects of the early English
written tradition, but even as he studied these classics he was
creating a set of his own.'
})
Querying for our author
> db.author.findOne( { last_name : 'Tolkien' } )
{
    "_id" : ObjectId("507ffbb1d94ccab2da652597"),
    "first_name" : "J.R.R.",
    "last_name" : "Tolkien",
    "bio" : "J.R.R. Tolkien (1892-1973), beloved throughout the
world as the creator of The Hobbit and The Lord of the Rings, was a
professor of Anglo-Saxon at Oxford, a fellow of Pembroke College,
and a fellow of Merton College until his retirement in 1959. His chief
interest was the linguistic aspects of the early English written
tradition, but even as he studied these classics he was creating a
set of his own."
}
Creating a Book
> db.books.insert({
            title: ‘Fellowship of the Ring, The',
            author: ObjectId("507ffbb1d94ccab2da652597"),
            language: 'English',
            genre: ['fantasy', 'adventure'],
            publication: {
                      name: 'George Allen & Unwin',
                      location: 'London',
      date: new Date('21 July 1954'),
            }
})

                                     http://society6.com/PastaSoup/The-Fellowship-of-the-Ring-ZZc_Print/
Multiple values per key
> db.books.findOne({language: 'English'}, {genre: 1})
{
    "_id" : ObjectId("50804391d94ccab2da652598"),
    "genre" : [
        "fantasy",
        "adventure"
    ]
}
Querying for key with
multiple values
> db.books.findOne({genre: 'fantasy'}, {title: 1})
{
    "_id" : ObjectId("50804391d94ccab2da652598"),
    "title" : "Fellowship of the Ring, The"
}




                     Query key with single value or
                     multiple values the same way.
Nested Values
> db.books.findOne({}, {publication: 1})
{
    "_id" : ObjectId("50804ec7d94ccab2da65259a"),
    "publication" : {
            "name" : "George Allen & Unwin",
            "location" : "London",
            "date" : ISODate("1954-07-21T04:00:00Z")
    }
}
Reach into nested values
using dot notation
> db.books.findOne(
    {'publication.date' :
              { $lt : new Date('21 June 1960')}
    }
)
{
    "_id" : ObjectId("50804391d94ccab2da652598"),
    "title" : "Fellowship of the Ring, The",
    "author" : ObjectId("507ffbb1d94ccab2da652597"),
    "language" : "english",
    "genre" : [ "fantasy",     "adventure" ],
    "publication" : {
              "name" : "george allen & unwin",
              "location" : "London",
              "date" : ISODate("1954-07-21T04:00:00Z")
    }
}
Update books
> db.books.update(
         {"_id" :
      ObjectId("50804391d94ccab2da652598")},
         { $set : {
                          isbn: '0547928211',
                          pages: 432
                      }
 })
                  True agile development .
                  Simply change how you work with
                  the data and the database follows
The Updated Book record
db.books.findOne()
{
    "_id" : ObjectId("50804ec7d94ccab2da65259a"),
    "author" : ObjectId("507ffbb1d94ccab2da652597"),
    "genre" : [ "fantasy", "adventure" ],
    "isbn" : "0395082544",
    "language" : "English",
    "pages" : 432,
    "publication" : {
              "name" : "George Allen & Unwin",
              "location" : "London",
              "date" : ISODate("1954-07-21T04:00:00Z")
    },
    "title" : "Fellowship of the Ring, The"
}
Creating indexes
> db.books.ensureIndex({title: 1})


> db.books.ensureIndex({genre : 1})


> db.books.ensureIndex({'publication.date': -1})
Querying with RegEx
> db.books.findOne({title : /^Fell/})
{
    "_id" : ObjectId("50804ec7d94ccab2da65259a"),
    "author" : ObjectId("507ffbb1d94ccab2da652597"),
    "genre" : [ "fantasy",     "adventure"   ],
    "isbn" : "0395082544",
    "language" : "English",
    "pages" : 432,
    "publication" : {
              "name" : "George Allen & Unwin",
              "location" : "London",
              "date" : ISODate("1954-07-21T04:00:00Z")
    },
    "title" : "Fellowship of the Ring, The"
}
Adding a few more books
> db.books.insert({
             title: 'Two Towers, The',
             author: ObjectId("507ffbb1d94ccab2da652597"),
             language: 'English',
             isbn : "034523510X",
             genre: ['fantasy', 'adventure'],
             pages: 447,
             publication: {
                       name: 'George Allen & Unwin',
                       location: 'London',
      date: new Date('11 Nov 1954'),
             }
})


                                       http://society6.com/PastaSoup/The-Two-Towers-XTr_Print/
Adding a few more books
> db.books.insert({
             title: 'Return of the King, The',
             author: ObjectId("507ffbb1d94ccab2da652597"),
             language: 'English',
             isbn : "0345248295",
             genre: ['fantasy', 'adventure'],
             pages: 544,
             publication: {
                        name: 'George Allen & Unwin',
                        location: 'London',
      date: new Date('20 Oct 1955'),
             }
})


                                     http://society6.com/PastaSoup/The-Return-of-the-King-Jsc_Print/
Cursors
> db.books.find(
{ author: ObjectId("507ffbb1d94ccab2da652597")})
.sort({ 'publication.date' : -1})
.limit(1)
{
     "_id" : ObjectId("5080d33ed94ccab2da65259d"),
     "title" : "Return of the King, The",
     "author" : ObjectId("507ffbb1d94ccab2da652597"),
     "language" : "English",
     "isbn" : "0345248295",
     "genre" : [ "fantasy", "adventure" ],
     "pages" : 544,
     "publication" : {
               "name" : "George Allen & Unwin",
               "location" : "London",
               "date" : ISODate("1955-10-20T04:00:00Z")
     }
}
Paging
page_num = 3;
results_per_page = 10;

cursor =
db.books.find()
 .sort({ "publication.date" : -1 })

.skip((page_num - 1) * results_per_page)

.limit(results_per_page);
Finding author by book
> book = db.books.findOne(
            {"title" : "Return of the King, The"})

> db.author.findOne({_id: book.author})
{
     "_id" : ObjectId("507ffbb1d94ccab2da652597"),
     "first_name" : "J.R.R.",
     "last_name" : "Tolkien",
     "bio" : "J.R.R. Tolkien (1892.1973), beloved throughout the world as
the creator of The Hobbit and The Lord of the Rings, was a professor of
Anglo-Saxon at Oxford, a fellow of Pembroke College, and a fellow of
Merton College until his retirement in 1959. His chief interest was the
linguistic aspects of the early English written tradition, but even as he
studied these classics he was creating a set of his own."
}
MongoDB Drivers
Real applications are not
built in the shell
MongoDB has native
bindings for over 12
languages
Building Your First App with MongoDB
Building Your First App with MongoDB
MongoDB drivers
• Official Support for 12 languages
• Community drivers for tons more
• Drivers connect to MongoDB servers
• Drivers translate BSON into native types
• MongoDB shell is not a driver, but works like one
 in some ways
• Installed using typical means (npm, cpan, gem,
 pip)
Next Steps
We've introduced a lot of
concepts here
Schema Design @ 10:45 am
Indexing/Query Optimization
@ 11:40 am
Replication @ 1:55 pm
Sharding @ 2:40 pm
$project   $unwind   $group




Aggregation @ 4:25 pm
#MongoBoston
                    Schema Design     @ 10:45 am
                          Indexing    @ 11:40 am
                        Replication   @ 1:55 pm
                         Sharding     @ 2:40 pm
                       Aggregation    @ 4:25 pm

Questions?
Mike Friedman
Perl Engineer & Evangelist, 10gen

More Related Content

What's hot (19)

Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
Back to Basics Webinar 1 - Introduction to NoSQL
Back to Basics Webinar 1 - Introduction to NoSQLBack to Basics Webinar 1 - Introduction to NoSQL
Back to Basics Webinar 1 - Introduction to NoSQL
Joe Drumgoole
 
JSON-(JavaScript Object Notation)
JSON-(JavaScript Object Notation)JSON-(JavaScript Object Notation)
JSON-(JavaScript Object Notation)
Skillwise Group
 
Data Modeling for the Real World
Data Modeling for the Real WorldData Modeling for the Real World
Data Modeling for the Real World
Mike Friedman
 
Building Your First MongoDB Application
Building Your First MongoDB ApplicationBuilding Your First MongoDB Application
Building Your First MongoDB Application
Tugdual Grall
 
San Francisco Java User Group
San Francisco Java User GroupSan Francisco Java User Group
San Francisco Java User Group
kchodorow
 
Realm to Json & Royal
Realm to Json & RoyalRealm to Json & Royal
Realm to Json & Royal
Leonardo Taehwan Kim
 
Building Apps with MongoDB
Building Apps with MongoDBBuilding Apps with MongoDB
Building Apps with MongoDB
Nate Abele
 
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB
 
Redis data modeling examples
Redis data modeling examplesRedis data modeling examples
Redis data modeling examples
Terry Cho
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
Steven Francia
 
Indexing
IndexingIndexing
Indexing
Mike Dirolf
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
Algiers Tech Meetup
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patterns
joergreichert
 
An Overview of HTML5 Storage
An Overview of HTML5 StorageAn Overview of HTML5 Storage
An Overview of HTML5 Storage
Paul Irish
 
Schema design
Schema designSchema design
Schema design
christkv
 
MongoDB
MongoDBMongoDB
MongoDB
Steve Klabnik
 
Back to Basics Webinar 5: Introduction to the Aggregation Framework
Back to Basics Webinar 5: Introduction to the Aggregation FrameworkBack to Basics Webinar 5: Introduction to the Aggregation Framework
Back to Basics Webinar 5: Introduction to the Aggregation Framework
MongoDB
 
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial IndexesBack to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
MongoDB
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB Application
MongoDB
 
Back to Basics Webinar 1 - Introduction to NoSQL
Back to Basics Webinar 1 - Introduction to NoSQLBack to Basics Webinar 1 - Introduction to NoSQL
Back to Basics Webinar 1 - Introduction to NoSQL
Joe Drumgoole
 
JSON-(JavaScript Object Notation)
JSON-(JavaScript Object Notation)JSON-(JavaScript Object Notation)
JSON-(JavaScript Object Notation)
Skillwise Group
 
Data Modeling for the Real World
Data Modeling for the Real WorldData Modeling for the Real World
Data Modeling for the Real World
Mike Friedman
 
Building Your First MongoDB Application
Building Your First MongoDB ApplicationBuilding Your First MongoDB Application
Building Your First MongoDB Application
Tugdual Grall
 
San Francisco Java User Group
San Francisco Java User GroupSan Francisco Java User Group
San Francisco Java User Group
kchodorow
 
Building Apps with MongoDB
Building Apps with MongoDBBuilding Apps with MongoDB
Building Apps with MongoDB
Nate Abele
 
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB San Francisco 2013: Data Modeling Examples From the Real World presen...
MongoDB
 
Redis data modeling examples
Redis data modeling examplesRedis data modeling examples
Redis data modeling examples
Terry Cho
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
Steven Francia
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patterns
joergreichert
 
An Overview of HTML5 Storage
An Overview of HTML5 StorageAn Overview of HTML5 Storage
An Overview of HTML5 Storage
Paul Irish
 
Schema design
Schema designSchema design
Schema design
christkv
 
Back to Basics Webinar 5: Introduction to the Aggregation Framework
Back to Basics Webinar 5: Introduction to the Aggregation FrameworkBack to Basics Webinar 5: Introduction to the Aggregation Framework
Back to Basics Webinar 5: Introduction to the Aggregation Framework
MongoDB
 
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial IndexesBack to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
MongoDB
 

Similar to Building Your First App with MongoDB (20)

Agile Schema Design: An introduction to MongoDB
Agile Schema Design: An introduction to MongoDBAgile Schema Design: An introduction to MongoDB
Agile Schema Design: An introduction to MongoDB
Stennie Steneker
 
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdfbuildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
lallababa
 
Aggregation Framework
Aggregation FrameworkAggregation Framework
Aggregation Framework
MongoDB
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
Mongo db japan
Mongo db japanMongo db japan
Mongo db japan
rogerbodamer
 
MongoDB @ Frankfurt NoSql User Group
MongoDB @  Frankfurt NoSql User GroupMongoDB @  Frankfurt NoSql User Group
MongoDB @ Frankfurt NoSql User Group
Chris Harris
 
Building a MongoDB App with Perl
Building a MongoDB App with PerlBuilding a MongoDB App with Perl
Building a MongoDB App with Perl
Mike Friedman
 
Latinoware
LatinowareLatinoware
Latinoware
kchodorow
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling
DATAVERSITY
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
MongoDB - Ruby document store that doesn't rhyme with ouch
MongoDB - Ruby document store that doesn't rhyme with ouchMongoDB - Ruby document store that doesn't rhyme with ouch
MongoDB - Ruby document store that doesn't rhyme with ouch
Wynn Netherland
 
Schema design mongo_boston
Schema design mongo_bostonSchema design mongo_boston
Schema design mongo_boston
MongoDB
 
Schema & Design
Schema & DesignSchema & Design
Schema & Design
MongoDB
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
MongoDB for Perl Developers
MongoDB for Perl DevelopersMongoDB for Perl Developers
MongoDB for Perl Developers
Ynon Perek
 
MongoDB For C++ Developers
MongoDB For C++ DevelopersMongoDB For C++ Developers
MongoDB For C++ Developers
Ynon Perek
 
Mongo db
Mongo dbMongo db
Mongo db
Toki Kanno
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
Agile Schema Design: An introduction to MongoDB
Agile Schema Design: An introduction to MongoDBAgile Schema Design: An introduction to MongoDB
Agile Schema Design: An introduction to MongoDB
Stennie Steneker
 
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdfbuildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
lallababa
 
Aggregation Framework
Aggregation FrameworkAggregation Framework
Aggregation Framework
MongoDB
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
MongoDB @ Frankfurt NoSql User Group
MongoDB @  Frankfurt NoSql User GroupMongoDB @  Frankfurt NoSql User Group
MongoDB @ Frankfurt NoSql User Group
Chris Harris
 
Building a MongoDB App with Perl
Building a MongoDB App with PerlBuilding a MongoDB App with Perl
Building a MongoDB App with Perl
Mike Friedman
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling10gen Presents Schema Design and Data Modeling
10gen Presents Schema Design and Data Modeling
DATAVERSITY
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
MongoDB - Ruby document store that doesn't rhyme with ouch
MongoDB - Ruby document store that doesn't rhyme with ouchMongoDB - Ruby document store that doesn't rhyme with ouch
MongoDB - Ruby document store that doesn't rhyme with ouch
Wynn Netherland
 
Schema design mongo_boston
Schema design mongo_bostonSchema design mongo_boston
Schema design mongo_boston
MongoDB
 
Schema & Design
Schema & DesignSchema & Design
Schema & Design
MongoDB
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
MongoDB for Perl Developers
MongoDB for Perl DevelopersMongoDB for Perl Developers
MongoDB for Perl Developers
Ynon Perek
 
MongoDB For C++ Developers
MongoDB For C++ DevelopersMongoDB For C++ Developers
MongoDB For C++ Developers
Ynon Perek
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 
Ad

More from Mike Friedman (7)

Basic Symbolic Computation in Perl
Basic Symbolic Computation in PerlBasic Symbolic Computation in Perl
Basic Symbolic Computation in Perl
Mike Friedman
 
Make Your Own Perl with Moops
Make Your Own Perl with MoopsMake Your Own Perl with Moops
Make Your Own Perl with Moops
Mike Friedman
 
The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)
Mike Friedman
 
21st Century CPAN Testing: CPANci
21st Century CPAN Testing: CPANci21st Century CPAN Testing: CPANci
21st Century CPAN Testing: CPANci
Mike Friedman
 
CPANci: Continuous Integration for CPAN
CPANci: Continuous Integration for CPANCPANci: Continuous Integration for CPAN
CPANci: Continuous Integration for CPAN
Mike Friedman
 
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
Mike Friedman
 
Building Scalable, Distributed Job Queues with Redis and Redis::Client
Building Scalable, Distributed Job Queues with Redis and Redis::ClientBuilding Scalable, Distributed Job Queues with Redis and Redis::Client
Building Scalable, Distributed Job Queues with Redis and Redis::Client
Mike Friedman
 
Basic Symbolic Computation in Perl
Basic Symbolic Computation in PerlBasic Symbolic Computation in Perl
Basic Symbolic Computation in Perl
Mike Friedman
 
Make Your Own Perl with Moops
Make Your Own Perl with MoopsMake Your Own Perl with Moops
Make Your Own Perl with Moops
Mike Friedman
 
The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)The Perl API for the Mortally Terrified (beta)
The Perl API for the Mortally Terrified (beta)
Mike Friedman
 
21st Century CPAN Testing: CPANci
21st Century CPAN Testing: CPANci21st Century CPAN Testing: CPANci
21st Century CPAN Testing: CPANci
Mike Friedman
 
CPANci: Continuous Integration for CPAN
CPANci: Continuous Integration for CPANCPANci: Continuous Integration for CPAN
CPANci: Continuous Integration for CPAN
Mike Friedman
 
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
Mike Friedman
 
Building Scalable, Distributed Job Queues with Redis and Redis::Client
Building Scalable, Distributed Job Queues with Redis and Redis::ClientBuilding Scalable, Distributed Job Queues with Redis and Redis::Client
Building Scalable, Distributed Job Queues with Redis and Redis::Client
Mike Friedman
 
Ad

Building Your First App with MongoDB

  • 1. #MongoBoston Building your first app: an introduction to MongoDB Mike Friedman Perl Engineer & Evangelist, 10gen
  • 3. MongoDB is a ___________ database • Document • Open source • High performance • Horizontally scalable • Full featured
  • 4. Document Database • Not for .PDF & .DOC files • A document is essentially an associative array • Document == JSON Object • Document == Perl Hash • Document == Python Dict • Document == Ruby Hash • etc.
  • 5. Open Source • MongoDB is an open source project • On GitHub • Server licensed under AGPL • Drivers licensed under Apache • Started & sponsored by 10gen • Commercial licenses available • Contributions welcome
  • 6. High Performance • Written in C++ • Extensive use of memory-mapped files i.e. read-through write-through memory caching. • Runs nearly everywhere • Data serialized as BSON (fast parsing) • Full support for primary & secondary indexes • Document model = less work
  • 8. Full Featured • Ad Hoc queries • Real time aggregation • Rich query capabilities • Geospatial features • Support for most programming languages • Flexible schema
  • 12. RDBMS MongoDB Table, View ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard Terminology
  • 15. We will build a library management application http://www.flickr.com/photos/somegeekintn/3484353131/
  • 16. First step in any application is Determine your entities
  • 17. Library Management Application Entities • Library Patrons (users) • Books (catalog) • Authors • Publishers • Categories ??
  • 18. In a relational based app We would start by doing schema design
  • 19. Relational schema design • Large ERD Diagrams • Complex create table statements • ORMs to map tables to objects • Tables just to join tables together • For this simple app we'd have 5 tables and 5 join tables • Lots of revisions until we get it just right
  • 20. In a MongoDB based app We start building our and let the schema evolve app
  • 23. Start with an object (or array, hash, dict, etc) user = { username: 'fred.jones', first_name: 'Fred', last_name: 'Jones', }
  • 24. Insert the record > db.users.insert(user) No collection creation needed
  • 25. Querying for the user > db.users.findOne() { "_id" : ObjectId("50804d0bd94ccab2da652599"), "username" : "fred.jones", "first_name" : "Fred", "last_name" : "Jones" }
  • 26. _id • _id is the primary key in MongoDB • Automatically indexed • Automatically created as an ObjectId if not provided • Any unique immutable value could be used
  • 27. ObjectId • ObjectId is a special 12 byte value • Guaranteed to be unique across your cluster • ObjectId("50804d0bd94ccab2da652599") Timestamp machine PID Increment
  • 28. Creating an author > db.author.insert({ first_name: ’J.R.R.', last_name: ‘Tolkien', bio: 'J.R.R. Tolkien (1892-1973), beloved throughout the world as the creator of The Hobbit and The Lord of the Rings, was a professor of Anglo-Saxon at Oxford, a fellow of Pembroke College, and a fellow of Merton College until his retirement in 1959. His chief interest was the linguistic aspects of the early English written tradition, but even as he studied these classics he was creating a set of his own.' })
  • 29. Querying for our author > db.author.findOne( { last_name : 'Tolkien' } ) { "_id" : ObjectId("507ffbb1d94ccab2da652597"), "first_name" : "J.R.R.", "last_name" : "Tolkien", "bio" : "J.R.R. Tolkien (1892-1973), beloved throughout the world as the creator of The Hobbit and The Lord of the Rings, was a professor of Anglo-Saxon at Oxford, a fellow of Pembroke College, and a fellow of Merton College until his retirement in 1959. His chief interest was the linguistic aspects of the early English written tradition, but even as he studied these classics he was creating a set of his own." }
  • 30. Creating a Book > db.books.insert({ title: ‘Fellowship of the Ring, The', author: ObjectId("507ffbb1d94ccab2da652597"), language: 'English', genre: ['fantasy', 'adventure'], publication: { name: 'George Allen & Unwin', location: 'London', date: new Date('21 July 1954'), } }) http://society6.com/PastaSoup/The-Fellowship-of-the-Ring-ZZc_Print/
  • 31. Multiple values per key > db.books.findOne({language: 'English'}, {genre: 1}) { "_id" : ObjectId("50804391d94ccab2da652598"), "genre" : [ "fantasy", "adventure" ] }
  • 32. Querying for key with multiple values > db.books.findOne({genre: 'fantasy'}, {title: 1}) { "_id" : ObjectId("50804391d94ccab2da652598"), "title" : "Fellowship of the Ring, The" } Query key with single value or multiple values the same way.
  • 33. Nested Values > db.books.findOne({}, {publication: 1}) { "_id" : ObjectId("50804ec7d94ccab2da65259a"), "publication" : { "name" : "George Allen & Unwin", "location" : "London", "date" : ISODate("1954-07-21T04:00:00Z") } }
  • 34. Reach into nested values using dot notation > db.books.findOne( {'publication.date' : { $lt : new Date('21 June 1960')} } ) { "_id" : ObjectId("50804391d94ccab2da652598"), "title" : "Fellowship of the Ring, The", "author" : ObjectId("507ffbb1d94ccab2da652597"), "language" : "english", "genre" : [ "fantasy", "adventure" ], "publication" : { "name" : "george allen & unwin", "location" : "London", "date" : ISODate("1954-07-21T04:00:00Z") } }
  • 35. Update books > db.books.update( {"_id" : ObjectId("50804391d94ccab2da652598")}, { $set : { isbn: '0547928211', pages: 432 } }) True agile development . Simply change how you work with the data and the database follows
  • 36. The Updated Book record db.books.findOne() { "_id" : ObjectId("50804ec7d94ccab2da65259a"), "author" : ObjectId("507ffbb1d94ccab2da652597"), "genre" : [ "fantasy", "adventure" ], "isbn" : "0395082544", "language" : "English", "pages" : 432, "publication" : { "name" : "George Allen & Unwin", "location" : "London", "date" : ISODate("1954-07-21T04:00:00Z") }, "title" : "Fellowship of the Ring, The" }
  • 37. Creating indexes > db.books.ensureIndex({title: 1}) > db.books.ensureIndex({genre : 1}) > db.books.ensureIndex({'publication.date': -1})
  • 38. Querying with RegEx > db.books.findOne({title : /^Fell/}) { "_id" : ObjectId("50804ec7d94ccab2da65259a"), "author" : ObjectId("507ffbb1d94ccab2da652597"), "genre" : [ "fantasy", "adventure" ], "isbn" : "0395082544", "language" : "English", "pages" : 432, "publication" : { "name" : "George Allen & Unwin", "location" : "London", "date" : ISODate("1954-07-21T04:00:00Z") }, "title" : "Fellowship of the Ring, The" }
  • 39. Adding a few more books > db.books.insert({ title: 'Two Towers, The', author: ObjectId("507ffbb1d94ccab2da652597"), language: 'English', isbn : "034523510X", genre: ['fantasy', 'adventure'], pages: 447, publication: { name: 'George Allen & Unwin', location: 'London', date: new Date('11 Nov 1954'), } }) http://society6.com/PastaSoup/The-Two-Towers-XTr_Print/
  • 40. Adding a few more books > db.books.insert({ title: 'Return of the King, The', author: ObjectId("507ffbb1d94ccab2da652597"), language: 'English', isbn : "0345248295", genre: ['fantasy', 'adventure'], pages: 544, publication: { name: 'George Allen & Unwin', location: 'London', date: new Date('20 Oct 1955'), } }) http://society6.com/PastaSoup/The-Return-of-the-King-Jsc_Print/
  • 41. Cursors > db.books.find( { author: ObjectId("507ffbb1d94ccab2da652597")}) .sort({ 'publication.date' : -1}) .limit(1) { "_id" : ObjectId("5080d33ed94ccab2da65259d"), "title" : "Return of the King, The", "author" : ObjectId("507ffbb1d94ccab2da652597"), "language" : "English", "isbn" : "0345248295", "genre" : [ "fantasy", "adventure" ], "pages" : 544, "publication" : { "name" : "George Allen & Unwin", "location" : "London", "date" : ISODate("1955-10-20T04:00:00Z") } }
  • 42. Paging page_num = 3;
results_per_page = 10;

cursor = db.books.find()
 .sort({ "publication.date" : -1 })
 .skip((page_num - 1) * results_per_page)
 .limit(results_per_page);
  • 43. Finding author by book > book = db.books.findOne( {"title" : "Return of the King, The"}) > db.author.findOne({_id: book.author}) { "_id" : ObjectId("507ffbb1d94ccab2da652597"), "first_name" : "J.R.R.", "last_name" : "Tolkien", "bio" : "J.R.R. Tolkien (1892.1973), beloved throughout the world as the creator of The Hobbit and The Lord of the Rings, was a professor of Anglo-Saxon at Oxford, a fellow of Pembroke College, and a fellow of Merton College until his retirement in 1959. His chief interest was the linguistic aspects of the early English written tradition, but even as he studied these classics he was creating a set of his own." }
  • 45. Real applications are not built in the shell
  • 46. MongoDB has native bindings for over 12 languages
  • 49. MongoDB drivers • Official Support for 12 languages • Community drivers for tons more • Drivers connect to MongoDB servers • Drivers translate BSON into native types • MongoDB shell is not a driver, but works like one in some ways • Installed using typical means (npm, cpan, gem, pip)
  • 51. We've introduced a lot of concepts here
  • 52. Schema Design @ 10:45 am
  • 56. $project $unwind $group Aggregation @ 4:25 pm
  • 57. #MongoBoston Schema Design @ 10:45 am Indexing @ 11:40 am Replication @ 1:55 pm Sharding @ 2:40 pm Aggregation @ 4:25 pm Questions? Mike Friedman Perl Engineer & Evangelist, 10gen

Editor's Notes

  • #2: Welcome toMongoDB BostonMy name is Mike FriedmanScratches surface of MongoDB
  • #5: These words do mean something
  • #8: MongoDB server written in C++ and is fastWorking sets kept in memory as much as possibleAny OSFast serialization formatIndexingLow development time
  • #9: Replica sets for redundancySharding for query distributionShards on top of replica sets
  • #10: Query optimization talk (11:40 w/ Tyler Brock)Aggregation talk (4:25 w/Jeremy)Dozen 10gen-supported drivers / community driversSo where do we get MongoDB?
  • #11: Version numberHow are we going to work with MongoDB?
  • #12: mongod is the MongoDB daemonFirst figure out what a document database is
  • #14: Not synonyms, but analogous
  • #15: Does not show join tables
  • #16: Notice square brackets on Comments, etc.CD app story
  • #19: Ask question: Is categories it's own entity? It could be, but it's likely a property of books.
  • #21: No "create table" – collections have no schema; all the same
  • #22: So what collections should we make? Only need three for now
  • #24: Now we will use the shell
  • #25: JSON format because shell is JavaScript
  • #26: Since all collections are the same, no need to createHow do we get it out?
  • #27: Object ID is created automatically
  • #28: Can not change the _id of a documentYou could delete it and create a new one
  • #29: 4 byte signed epoch;3 bytes of MD5 hash of host16,777,216
  • #30: Powerful message here. Finally a database that enables rapid & agile development.
  • #31: Now we are specifying a search fieldHave user, have author, insert a book
  • #32: Embedded array for genreEmbedded doc for publicationDate object
  • #33: Search on languageOnly return genre_id comes back by default
  • #34: genre is an array
  • #35: First document empty, so finds any documentOnly returns publication embedded docCan we query fields in nested docs?
  • #36: Nested fields with dotFirst instance of “dollar operators”How do we update?
  • #37: Second document gives data to update/replaceDollar operator $set updates/adds individual fieldsOtherwise update takes a whole document to replace
  • #38: MongoDB documents behave like arbitrary hashes/maps in your programming languageHow do we deal with getting big?
  • #39: -1 means descendingIndexing/Query opt Tyler @ 11:40am
  • #40: Let's add more
  • #41: Creating a book here. A few things to make note of.
  • #42: Redundant publication stuff
  • #43: Author query returns cursor; RoTK most recentWhat else w/cursors?
  • #44: Skip 20Results 21-30
  • #49: Official 10gen drivers
  • #50: Community driversProlog, D, ColdFusion, R, MatlabMore than shown here
  • #53: MongoDB is different Evaluate tools by being educated
  • #54: Schema Design @10:45 with Chad, Solution Architect
  • #55: Indexing @11:40 with Tyler, Ruby driver devIndexing is essential for mission-critical apps with large data requirements
  • #56: Repl @ 1:55 with Steve, head of Evangelism Replication is essential for redundancy
  • #57: Shard @ 2:40 with Jared, Dir of Product MarketingSharding is essential for fast access
  • #58: Aggregation @4:25 with Jeremy, Drivers, PHP