SlideShare ist ein Scribd-Unternehmen logo
1 von 59
#MongoDBDays


   Building Your First App:
     An Introduction to
          MongoDB
Brandon Black
Software Engineer, 10gen
@brandonmblack
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 == PHP Array
• Document == Python Dict
• Document == Ruby Hash
Open Source
• MongoDB is an open source project
• On GitHub
• Licensed under the AGPL
• 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
• Traditionally consistent
• Geospatial features
• Support for most programming languages
• Flexible schema
Database Landscape
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
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
• Publishers
No Common Language
Mongo Shell
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")
           |-------------||---------||-----||----------|
                 ts          mac pid inc
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"
    ]
}
Multiple Values Per Key
> 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")
    }
}
Query Nested Values with
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
        }
   })
                     This is true agile development .
                     I’m simply changing how I work
                     with the data, and the
                     database follows.
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})
Query with Regular
Expressions
> 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 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 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")
    }
}
Simple Pager
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 an 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 Drivers
• Drivers connect to mongo servers
• Drivers translate BSON into native types
• The MongoDB shell is not a driver, but works like
 one in some ways
• Installed using typical means (npm, pecl, gem,
 pip)
MongoDB has native
bindings for over 12
languages
Next Steps
We've introduced a lot of
concepts here
Schema Design @ 10:35am
Replication @ 12:15pm
Indexing @ 1:45pm
Sharding @ 2:30pm
• What’s next?
  –   Schema Design @ 10:35am
  –   Replication @ 12:15pm
  –   Indexing @ 1:45pm
  –   Sharding @ 2:30pm
  –   Webinar: Technical Overview of MongoDB (March 7th)
  –   MongoDB User Group
• Resources
       https://education.10gen.com/
       http://www.10gen.com/presentations
       http://github.com/brandonblack/presentations
#MongoDBDays




Thank You
Brandon Black
Software Engineer, 10gen
@brandonmblack

Weitere ähnliche Inhalte

Was ist angesagt?

San Francisco Java User Group
San Francisco Java User GroupSan Francisco Java User Group
San Francisco Java User Group
kchodorow
 

Was ist angesagt? (11)

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
 
Building Your First MongoDB Application
Building Your First MongoDB ApplicationBuilding Your First MongoDB Application
Building Your First MongoDB Application
 
Data Modeling for the Real World
Data Modeling for the Real WorldData Modeling for the Real World
Data Modeling for the Real World
 
Building Apps with MongoDB
Building Apps with MongoDBBuilding Apps with MongoDB
Building Apps with MongoDB
 
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...
 
San Francisco Java User Group
San Francisco Java User GroupSan Francisco Java User Group
San Francisco Java User Group
 
Introduction to MongoDB and Hadoop
Introduction to MongoDB and HadoopIntroduction to MongoDB and Hadoop
Introduction to MongoDB and Hadoop
 
Better Web Clients with Mantle and AFNetworking
Better Web Clients with Mantle and AFNetworkingBetter Web Clients with Mantle and AFNetworking
Better Web Clients with Mantle and AFNetworking
 
Couchdb w Ruby'm
Couchdb w Ruby'mCouchdb w Ruby'm
Couchdb w Ruby'm
 
Indexing
IndexingIndexing
Indexing
 
Mongo DB schema design patterns
Mongo DB schema design patternsMongo DB schema design patterns
Mongo DB schema design patterns
 

Andere mochten auch

Finding Love with MongoDB
Finding Love with MongoDBFinding Love with MongoDB
Finding Love with MongoDB
MongoDB
 
Data Modeling Examples from the Real World
Data Modeling Examples from the Real WorldData Modeling Examples from the Real World
Data Modeling Examples from the Real World
MongoDB
 
2011 mongo sf-roadmap
2011 mongo sf-roadmap2011 mongo sf-roadmap
2011 mongo sf-roadmap
MongoDB
 
Building a High-Performance Distributed Task Queue on MongoDB
Building a High-Performance Distributed Task Queue on MongoDBBuilding a High-Performance Distributed Task Queue on MongoDB
Building a High-Performance Distributed Task Queue on MongoDB
MongoDB
 
Chat in Space with MongoDB
Chat in Space with MongoDBChat in Space with MongoDB
Chat in Space with MongoDB
MongoDB
 
Business Track: How Criteo Scaled and Supported Massive Growth with MongoDB
Business Track: How Criteo Scaled and Supported Massive Growth with MongoDBBusiness Track: How Criteo Scaled and Supported Massive Growth with MongoDB
Business Track: How Criteo Scaled and Supported Massive Growth with MongoDB
MongoDB
 
Building Applications using MongoDB, Node.js and Socket.io
Building Applications using MongoDB, Node.js and Socket.ioBuilding Applications using MongoDB, Node.js and Socket.io
Building Applications using MongoDB, Node.js and Socket.io
MongoDB
 
MongoDB Hacks of Frustration
MongoDB Hacks of FrustrationMongoDB Hacks of Frustration
MongoDB Hacks of Frustration
MongoDB
 
How We Fixed Our MongoDB Problems
How We Fixed Our MongoDB Problems How We Fixed Our MongoDB Problems
How We Fixed Our MongoDB Problems
MongoDB
 
Introduction: State of MongoDB in Malaysia
Introduction: State of MongoDB in MalaysiaIntroduction: State of MongoDB in Malaysia
Introduction: State of MongoDB in Malaysia
MongoDB
 

Andere mochten auch (18)

Finding Love with MongoDB
Finding Love with MongoDBFinding Love with MongoDB
Finding Love with MongoDB
 
Data Modeling Examples from the Real World
Data Modeling Examples from the Real WorldData Modeling Examples from the Real World
Data Modeling Examples from the Real World
 
2011 mongo sf-roadmap
2011 mongo sf-roadmap2011 mongo sf-roadmap
2011 mongo sf-roadmap
 
Social Content Management with MongoDB
Social Content Management with MongoDBSocial Content Management with MongoDB
Social Content Management with MongoDB
 
MongoDB at Giant Eagle by David Williams
MongoDB at Giant Eagle by David WilliamsMongoDB at Giant Eagle by David Williams
MongoDB at Giant Eagle by David Williams
 
MongoDB, ANTS, and the IC
MongoDB, ANTS, and the ICMongoDB, ANTS, and the IC
MongoDB, ANTS, and the IC
 
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
MongoDB San Francisco 2013: Schema design presented by Jason Zucchetto, Consu...
 
Fast REST APIs Development with MongoDB
Fast REST APIs Development with MongoDBFast REST APIs Development with MongoDB
Fast REST APIs Development with MongoDB
 
Get your Spatial on with MongoDB in the Cloud
Get your Spatial on with MongoDB in the CloudGet your Spatial on with MongoDB in the Cloud
Get your Spatial on with MongoDB in the Cloud
 
Building a High-Performance Distributed Task Queue on MongoDB
Building a High-Performance Distributed Task Queue on MongoDBBuilding a High-Performance Distributed Task Queue on MongoDB
Building a High-Performance Distributed Task Queue on MongoDB
 
Chat in Space with MongoDB
Chat in Space with MongoDBChat in Space with MongoDB
Chat in Space with MongoDB
 
Business Track: How Criteo Scaled and Supported Massive Growth with MongoDB
Business Track: How Criteo Scaled and Supported Massive Growth with MongoDBBusiness Track: How Criteo Scaled and Supported Massive Growth with MongoDB
Business Track: How Criteo Scaled and Supported Massive Growth with MongoDB
 
Building Applications using MongoDB, Node.js and Socket.io
Building Applications using MongoDB, Node.js and Socket.ioBuilding Applications using MongoDB, Node.js and Socket.io
Building Applications using MongoDB, Node.js and Socket.io
 
MongoDB Hacks of Frustration
MongoDB Hacks of FrustrationMongoDB Hacks of Frustration
MongoDB Hacks of Frustration
 
MongoDB at Medtronic
MongoDB at MedtronicMongoDB at Medtronic
MongoDB at Medtronic
 
How We Fixed Our MongoDB Problems
How We Fixed Our MongoDB Problems How We Fixed Our MongoDB Problems
How We Fixed Our MongoDB Problems
 
Indexing documents
Indexing documentsIndexing documents
Indexing documents
 
Introduction: State of MongoDB in Malaysia
Introduction: State of MongoDB in MalaysiaIntroduction: State of MongoDB in Malaysia
Introduction: State of MongoDB in Malaysia
 

Ähnlich wie Building Your First App: An Introduction to MongoDB

buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdfbuildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
lallababa
 
C# Development (Sam Corder)
C# Development (Sam Corder)C# Development (Sam Corder)
C# Development (Sam Corder)
MongoSF
 
Schema Design
Schema DesignSchema Design
Schema Design
MongoDB
 

Ähnlich wie Building Your First App: An Introduction to MongoDB (16)

Building Your First App with MongoDB
Building Your First App with MongoDBBuilding Your First App with MongoDB
Building Your First App with 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
 
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdfbuildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
buildyourfirstmongodbappberlin2013thomas-130313104259-phpapp02.pdf
 
ElasticSearch at berlinbuzzwords 2010
ElasticSearch at berlinbuzzwords 2010ElasticSearch at berlinbuzzwords 2010
ElasticSearch at berlinbuzzwords 2010
 
Memory Management & Debugging
Memory Management & DebuggingMemory Management & Debugging
Memory Management & Debugging
 
C# Development (Sam Corder)
C# Development (Sam Corder)C# Development (Sam Corder)
C# Development (Sam Corder)
 
運用 Exposed 管理及操作資料庫
運用 Exposed 管理及操作資料庫運用 Exposed 管理及操作資料庫
運用 Exposed 管理及操作資料庫
 
Dev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best PracticesDev Jumpstart: Schema Design Best Practices
Dev Jumpstart: Schema Design Best Practices
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema Design
 
Schema design
Schema designSchema design
Schema design
 
Flash Build: Understanding Shakespeare - ITHAKA Sustainable Scholarship 2014
Flash Build: Understanding Shakespeare - ITHAKA Sustainable Scholarship 2014Flash Build: Understanding Shakespeare - ITHAKA Sustainable Scholarship 2014
Flash Build: Understanding Shakespeare - ITHAKA Sustainable Scholarship 2014
 
Mongo DB
Mongo DBMongo DB
Mongo DB
 
Ast transformations
Ast transformationsAst transformations
Ast transformations
 
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
 
Schema Design
Schema DesignSchema Design
Schema Design
 
There and Back Again
There and Back AgainThere and Back Again
There and Back Again
 

Mehr von MongoDB

Mehr von MongoDB (20)

MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 MongoDB SoCal 2020: MongoDB Atlas Jump Start MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
 

Building Your First App: An Introduction to MongoDB

  • 1. #MongoDBDays Building Your First App: An Introduction to MongoDB Brandon Black Software Engineer, 10gen @brandonmblack
  • 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 == PHP Array • Document == Python Dict • Document == Ruby Hash
  • 5. Open Source • MongoDB is an open source project • On GitHub • Licensed under the AGPL • 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 • Traditionally consistent • Geospatial features • Support for most programming languages • Flexible schema
  • 13. RDBMS MongoDB Table, View ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard Terminology
  • 16. Library Management Application http://www.flickr.com/photos/somegeekintn/3484353131/
  • 17. First step in any application is Determine your entities
  • 18. Library Management Application Entities • Library Patrons (users) • Books (catalog) • Authors • Publishers • Categories ??
  • 19. In a relational based app We would start by doing schema design
  • 20. 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
  • 21. In a MongoDB based app We start building our and let the schema evolve app
  • 22. MongoDB Collections • Users • Books • Authors • Publishers
  • 25. Start with an Object (or array, hash, dict, etc) user = { username: 'fred.jones', first_name: 'fred', last_name: 'jones', }
  • 26. Insert the Record > db.users.insert(user) No collection creation needed
  • 27. Querying for the User > db.users.findOne() { "_id" : ObjectId("50804d0bd94ccab2da652599"), "username" : "fred.jones", "first_name" : "fred", "last_name" : "jones" }
  • 28. _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
  • 29. ObjectId • ObjectId is a special 12 byte value • Guaranteed to be unique across your cluster • ObjectId("50804d0bd94ccab2da652599") |-------------||---------||-----||----------| ts mac pid inc
  • 30. 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.' })
  • 31. 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." }
  • 32. 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/
  • 33. Multiple Values Per Key > db.books.findOne({language: 'english'}, {genre: 1}) { "_id" : ObjectId("50804391d94ccab2da652598"), "genre" : [ "fantasy", "adventure" ] }
  • 34. Multiple Values Per Key > 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.
  • 35. Nested Values > db.books.findOne({}, {publication: 1}) { "_id" : ObjectId("50804ec7d94ccab2da65259a"), "publication" : { "name" : "george allen & unwin", "location" : "London", "date" : ISODate("1954-07-21T04:00:00Z") } }
  • 36. Query Nested Values with 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") } }
  • 37. Update Books > db.books.update( {"_id" : ObjectId("50804391d94ccab2da652598")}, { $set : { isbn: '0547928211', pages: 432 } }) This is true agile development . I’m simply changing how I work with the data, and the database follows.
  • 38. 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" }
  • 39. Creating Indexes > db.books.ensureIndex({title: 1}) > db.books.ensureIndex({genre : 1}) > db.books.ensureIndex({'publication.date': -1})
  • 40. Query with Regular Expressions > 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" }
  • 41. Adding 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/
  • 42. Adding 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/
  • 43. 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") } }
  • 44. Simple Pager 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);
  • 45. Finding an 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." }
  • 47. Real applications are not built in the shell
  • 48. MongoDB Drivers • Drivers connect to mongo servers • Drivers translate BSON into native types • The MongoDB shell is not a driver, but works like one in some ways • Installed using typical means (npm, pecl, gem, pip)
  • 49. MongoDB has native bindings for over 12 languages
  • 50.
  • 51.
  • 53. We've introduced a lot of concepts here
  • 54. Schema Design @ 10:35am
  • 58. • What’s next? – Schema Design @ 10:35am – Replication @ 12:15pm – Indexing @ 1:45pm – Sharding @ 2:30pm – Webinar: Technical Overview of MongoDB (March 7th) – MongoDB User Group • Resources https://education.10gen.com/ http://www.10gen.com/presentations http://github.com/brandonblack/presentations
  • 59. #MongoDBDays Thank You Brandon Black Software Engineer, 10gen @brandonmblack

Hinweis der Redaktion

  1. Introduction and QuestionsHow many are currently using MongoDB?How many of you came from a relational database background?Emphasize the messagePreview of our talkLook at what makes MongoDB differentWalk through building your first application with MongoDB
  2. Let’s talk about what we mean by this….
  3. What do we mean by document?
  4. AGPL – GNU Affero General Public License
  5. * Big endian and ARM not supported.
  6. Where can you get it?
  7. Easy to use and interact withNo additional tools required, just jump right into the shell
  8. Entity-relationship diagram
  9. Kristine to update this graphic at some point
  10. We’re going to get nerdy hereThe books on this shelf should be a strong indication of what’s to comeIf you happen to be one of his arch enemies, apologies for the appearance of Wil Wheaton’s name
  11. Whats the first step?
  12. Ask question: Is categories it's own entity? It could be, but it's likely a property of books.
  13. So this is where the fun would end in a traditional relational database. Schema design slows you down. With mongodb, you do still need to think about how you want to store and access your data effectively, but there’s no up front commitment on the developers part. We can jump right into the building it.
  14. Ask question: Is categories it's own entity? It could be, but it's likely a property of books. What are some pain points of relational db schema design?
  15. This doesn’t mean we don’t think about schema, it just means that we let it evolve with out application. In MongoDB schemas are flexible and based on a different idea. Schemas are about how you use the data, not about how you store the data.So what kind of collections would be build in MongoDB…
  16. Point out entities and what we end up doing them. We don’t have to normalize them.Do we want to query by publishers? Or do we just want the publisher information along without books?
  17. I’m sure we have developers with many different backgrounds in the room today. Finding a common language would be challenging, so we’re going to use the Mongo Shell directly to build this app.
  18. What is that _id field? I didn’t add that?
  19. "50804d0bd94ccab2da652599" is a 24 byte string (12 byte ObjectId hex encoded).See how easy MongoDB makes this for you? Agile development etcLets get back to our app.
  20. Powerful message here. Finally a database that enables rapid & agile development.
  21. Creating a book here. A few things to make note of here…Author referenceGenre (List)Publication (Embedded Document)Date (new type, why not ISODate?)How would I query on genre? How do I work with keys/document fields that might have multiple values?
  22. Specify which fields are returned. Default is everything
  23. It’s all the same!
  24. Feels familiar, very intuitive.How about updating the book? What if I need to add something new to this object?
  25. Powerful message here. Finally a database that enables rapid & agile development.
  26. You can see the updated fields here.What about indexes? Right now, we only have an index on _id because MongoDB does that for us automatically.If I don’t have to create my collection ahead of time and specify a strict schema, how do I define indexes and optimize my queries?
  27. Creating a book here. A few things to make note of.
  28. Creating a book here. A few things to make note of.
  29. This is fine for small result sets. Not good performance for large result sets. Range query plus limit will give better performance.
  30. The shell is cool and all, but how do I use MongoDB in language X?
  31. Raise your hand if your language of choice is on this screen?
  32. Plus community drivers. How about this one?
  33. But its only the tip of the ice-berg….
  34. In this room with Craig Wilson
  35. Hannes Magnusson
  36. Kevin Hanson
  37. Yours truly.