SlideShare une entreprise Scribd logo
1  sur  53
Building your First App with
MongoDB


Blossom Coryat
Solutions Architect, 10gen
Today‟s Talk
• Introduction to MongoDB


• Discuss data modeling, queries, indexes,
 updates and aggregation in MongoDB within
 the context of building a location-based app
What is MongoDB?
MongoDB is a ___________
database
• Document Oriented
• Open source
• High performance
• Highly Available
• Horizontally scalable
• Full featured
Document Oriented
• A document is essentially an associative array
• Document == JSON object
• Document == PHP Array
• Document == Python Dict
• Document == Ruby Hash
• Etc.
Open Source
• MongoDB is an open source project
  – Started and maintained by 10gen

• Licensed under the AGPL
• Commercial licenses available
• On GitHub
• Contributions welcome
High Performance
• Written in C++
• Runs nearly everywhere
• Extensive use of memory-mapped files
• Data serialized as BSON (fast parsing)
• Full support for primary & secondary indexes
• Document model = less work
Full Featured
• Ad Hoc queries
• Real time aggregation
• Rich query capabilities
• Flexible schema
• Geospatial features
• Support for most programming languages
MongoDB drivers
• Official Support for 12 languages
• Community drivers for tons more
• Drivers connect to mongo servers
• Drivers translate BSON into native types
• Installed using typical means (npm, pecl, gem,
 pip)
Highly Available
Replica Sets
• Redundancy and
 Failover
• Multi-DC deployments
• Zero downtime for
 upgrades and
 maintenance
Horizontally Scalable
Sharding
• Partition your
 data
• Auto-balancing
• Scale write
 throughput
• Increase
 capacity
Document Database
RDBMS                 MongoDB
Row           ➜   Document
Table, View   ➜   Collection
Index         ➜   Index
Join          ➜   Embedded Document
Foreign       ➜   Reference
Key
Partition     ➜ Shard
Terminology
Typical (relational) ERD
MongoDB ERD
Example Document
{
        _id : ObjectId("4c4ba5c0672c685e5e8aabf3"),
         author : "Blossom",
         date : ISODate("2012-11-02T11:52:27.442Z"),
         title: "Intro to MongoDB",
         tags : [ "tech", "databases”, ”documents" ],
         comments : [{
                author : "Fred",
           date : ISODate("2012-11-03T17:22:21.124Z"),
           text : ”interesting article, lots of great nuggets of
                  wisdom"
          }],
          comment_count : 1
    }
Building a check-in web
app
Requirements
• Users should be able to check in to a location
• Should be able to view check-ins by location
• Ability to assign categories to places
• Find nearby places
• Generate check-in statistics
First step in any application is
Determine your entities
Check-in App Entities
• People checking in


• Places to check in to


• Check-in events
In a relational based app
We would start by doing
schema design
Relational schema design
• Large entity relationship diagrams
• Complex create table statements
• Tables just to join tables together
• ORMs to map tables to objects
• 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
• Places
• Check-ins
  – Separate collection to manage discrete check-in events
    and support high workload of writes
We‟ll use the JS shell to get
started
Start with an object
(or array, hash, dict, etc)

> user = {
              firstname: 'fred',
              lastname: 'jones',
}
Insert the record

> db.users.insert(user)




                  No collection creation
                  needed
Querying for the user
> db.users.findOne()
{
    "_id" : ObjectId("50804d0bd94ccab2da652599"),
    "firstname" : "fred",
    "lastname" : "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
Places v1
> db.places.insert({
    name: “Exploratorium”,
    address: “3601 Lyon St”
    city: “San Francisco”,
    postcode: 94123
}
Places v1
> db.places.insert({
    name: “Exploratorium”,
    address: “3601 Lyon St”
    city: “San Francisco”,
    postcode: 94123
}


> db.places.find({name:”Exploratorium”})
Places v2
> db.places.insert({
    name: “Exploratorium”,
    address: “3601 Lyon St”
    city: “San Francisco”,
    postcode: 94123,
    categories: [“Social”, “Arts”, “Museums”]
}
Places v2
> db.places.insert({
    name: “Exploratorium”,
    address: “3601 Lyon St”
    city: “San Francisco”,
    postcode: 94123,
    categories: [“Social”, “Arts”, “Museums”]
}


> db.places.ensureIndex({categories: 1})
> db.places.find({categories: ”Social”})
Places v3
> db.places.insert({
    name: “Exploratorium”,
    address: “3601 Lyon St”
    city: “San Francisco”,
    postcode: 94123,
    categories: [“Social”, “Arts”, “Museums”],
    location: [37.803963,-122.448531]
}


> db.places.ensureIndex({location: “2d”})
> db.places.find({location:{$near:[37.81,-122.45]}})
Indexes
• Can be created on any field
   – Including fields within sub-documents or embedded arrays

• Support for compound indexes on any
 combination of keys
• Indexes should exist for primary, common, and
 user-facing queries
Indexes on Places
// creating indexes
> db.places.ensureIndex({name:1})
> db.places.ensureIndex({categories:1})
> db.places.ensureIndex({location:”2d”})


// finding by regular expression
> db.places.find({name:/^explor/i})


// finding nearby
> db.places.find({location:{$near:[37.81,-122.45]}})


// finding by category
> db.places.find({categories: ”Social”})
Updating Documents
• MongoDB supports atomic, in-place document updates

• $inc

• $set, $unset

• $push, $pushAll

• $addToSet, $each

• $pop, $pull, $pullAll

• $rename

• $bit
Updating
> db.checkins.insert({
     place_id: pid,
     user_id: uid,
     datetime: new Date()
})


// update the “checkins” count on place and user
// “checkins” will be added if it doesn‟t exist


> db.places.update({_id: pid}, {$inc: {checkins: 1}})
> db.users.update({_id: uid}, {$inc: {checkins: 1}})
Simple Statistics
// find the last 10 checkins for a place:
> p = db.places.find({name:”Exploratorium”})
> db.checkins.find({place_id:p[„_id‟]})
    .sort({datetime:-1}).limit(10)


// how many people checked in today
> db.checkins.find({place_id:p[„_id‟],
    datetime: {$gt: midnight}}).count()
Aggregation Framework
• Documents pass through a pipeline of
 operations
• Mechanism to calculate aggregate values across
 your data
• Similar functionality to GROUP BY and related
 SQL operators
• Along with computation, data can be reshaped
   – Add computed fields
   – Create virtual sub-objects
   – Extract sub-fields
Aggregation Framework
Operators
• $project
• $match
• $limit
• $skip
• $unwind
• $group
• $sort
Advanced Statistics
// checkins per place per day of the week
> db.checkins.aggregate(
    {$project: {
          place_id:1,
          dotw: { $dayOfWeek: ”$datetime”}
    }},
    {$group: {
          _id: {place_id: “$place_id”, dotw: “$dotw”},
          count: {$sum: 1}
    }})
Deployment
Deployment

• Single server
 - need a strong backup plan
                               P
Deployment

• Single server
 - need a strong backup plan
                                   P

• Replica sets                 P   S   S
 - High availability
 - Automatic failover
Deployment

• Single server
 - need a strong backup plan
                                   P

• Replica sets                 P   S   S
 - High availability
 - Automatic failover

                               P   S   S
• Sharded
 - Horizontally scale
 - Auto balancing              P   S   S
Learn More:
10gen.com/presentations

education.10gen.com

Contenu connexe

Tendances

Your Data, Your Search, ElasticSearch (EURUKO 2011)
Your Data, Your Search, ElasticSearch (EURUKO 2011)Your Data, Your Search, ElasticSearch (EURUKO 2011)
Your Data, Your Search, ElasticSearch (EURUKO 2011)Karel Minarik
 
SDEC2011 NoSQL concepts and models
SDEC2011 NoSQL concepts and modelsSDEC2011 NoSQL concepts and models
SDEC2011 NoSQL concepts and modelsKorea Sdec
 
Webinar: Building Your First MongoDB App
Webinar: Building Your First MongoDB AppWebinar: Building Your First MongoDB App
Webinar: Building Your First MongoDB AppMongoDB
 
ElasticSearch - index server used as a document database
ElasticSearch - index server used as a document databaseElasticSearch - index server used as a document database
ElasticSearch - index server used as a document databaseRobert Lujo
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBMongoDB
 
Back to Basics, webinar 2: La tua prima applicazione MongoDB
Back to Basics, webinar 2: La tua prima applicazione MongoDBBack to Basics, webinar 2: La tua prima applicazione MongoDB
Back to Basics, webinar 2: La tua prima applicazione MongoDBMongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBNosh Petigara
 
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...Gianfranco Palumbo
 
Open Source Logging and Monitoring Tools
Open Source Logging and Monitoring ToolsOpen Source Logging and Monitoring Tools
Open Source Logging and Monitoring ToolsPhase2
 
OSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialOSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialSteven Francia
 
Elasticsearch first-steps
Elasticsearch first-stepsElasticsearch first-steps
Elasticsearch first-stepsMatteo Moci
 
Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011rogerbodamer
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBJustin Smestad
 
An Introduction to Elastic Search.
An Introduction to Elastic Search.An Introduction to Elastic Search.
An Introduction to Elastic Search.Jurriaan Persyn
 
High Performance Applications with MongoDB
High Performance Applications with MongoDBHigh Performance Applications with MongoDB
High Performance Applications with MongoDBMongoDB
 
Beyond the Basics 2: Aggregation Framework
Beyond the Basics 2: Aggregation Framework Beyond the Basics 2: Aggregation Framework
Beyond the Basics 2: Aggregation Framework MongoDB
 
Introduction to Elasticsearch with basics of Lucene
Introduction to Elasticsearch with basics of LuceneIntroduction to Elasticsearch with basics of Lucene
Introduction to Elasticsearch with basics of LuceneRahul Jain
 
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...Miguel Gallardo
 

Tendances (20)

Your Data, Your Search, ElasticSearch (EURUKO 2011)
Your Data, Your Search, ElasticSearch (EURUKO 2011)Your Data, Your Search, ElasticSearch (EURUKO 2011)
Your Data, Your Search, ElasticSearch (EURUKO 2011)
 
SDEC2011 NoSQL concepts and models
SDEC2011 NoSQL concepts and modelsSDEC2011 NoSQL concepts and models
SDEC2011 NoSQL concepts and models
 
Webinar: Building Your First MongoDB App
Webinar: Building Your First MongoDB AppWebinar: Building Your First MongoDB App
Webinar: Building Your First MongoDB App
 
ElasticSearch - index server used as a document database
ElasticSearch - index server used as a document databaseElasticSearch - index server used as a document database
ElasticSearch - index server used as a document database
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
Back to Basics, webinar 2: La tua prima applicazione MongoDB
Back to Basics, webinar 2: La tua prima applicazione MongoDBBack to Basics, webinar 2: La tua prima applicazione MongoDB
Back to Basics, webinar 2: La tua prima applicazione MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
 
Open Source Logging and Monitoring Tools
Open Source Logging and Monitoring ToolsOpen Source Logging and Monitoring Tools
Open Source Logging and Monitoring Tools
 
OSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialOSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB Tutorial
 
MongoDB
MongoDBMongoDB
MongoDB
 
Elasticsearch first-steps
Elasticsearch first-stepsElasticsearch first-steps
Elasticsearch first-steps
 
Elasticsearch 5.0
Elasticsearch 5.0Elasticsearch 5.0
Elasticsearch 5.0
 
Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011Mongo Web Apps: OSCON 2011
Mongo Web Apps: OSCON 2011
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
An Introduction to Elastic Search.
An Introduction to Elastic Search.An Introduction to Elastic Search.
An Introduction to Elastic Search.
 
High Performance Applications with MongoDB
High Performance Applications with MongoDBHigh Performance Applications with MongoDB
High Performance Applications with MongoDB
 
Beyond the Basics 2: Aggregation Framework
Beyond the Basics 2: Aggregation Framework Beyond the Basics 2: Aggregation Framework
Beyond the Basics 2: Aggregation Framework
 
Introduction to Elasticsearch with basics of Lucene
Introduction to Elasticsearch with basics of LuceneIntroduction to Elasticsearch with basics of Lucene
Introduction to Elasticsearch with basics of Lucene
 
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
Code decoupling from Symfony (and others frameworks) - PHP Conference Brasil ...
 

Similaire à Building your First App with MongoDB

OSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross LawleyOSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross LawleyNETWAYS
 
Mongodb intro
Mongodb introMongodb intro
Mongodb introchristkv
 
Building web applications with mongo db presentation
Building web applications with mongo db presentationBuilding web applications with mongo db presentation
Building web applications with mongo db presentationMurat Çakal
 
MongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewAntonio Pintus
 
Building APIs in an easy way using API Platform
Building APIs in an easy way using API PlatformBuilding APIs in an easy way using API Platform
Building APIs in an easy way using API PlatformAntonio Peric-Mazar
 
Learn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDBLearn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDBMarakana Inc.
 
Managing Social Content with MongoDB
Managing Social Content with MongoDBManaging Social Content with MongoDB
Managing Social Content with MongoDBMongoDB
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'tsThe Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'tsMatias Cascallares
 
10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCup10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCupWebGeek Philippines
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDBNorberto Leite
 
Webinar: What's new in the .NET Driver
Webinar: What's new in the .NET DriverWebinar: What's new in the .NET Driver
Webinar: What's new in the .NET DriverMongoDB
 
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
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBMongoDB
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsMongoDB
 
Building Your First MongoDB Application
Building Your First MongoDB ApplicationBuilding Your First MongoDB Application
Building Your First MongoDB ApplicationRick Copeland
 
9b. Document-Oriented Databases lab
9b. Document-Oriented Databases lab9b. Document-Oriented Databases lab
9b. Document-Oriented Databases labFabio Fumarola
 
MongoDB & NoSQL 101
 MongoDB & NoSQL 101 MongoDB & NoSQL 101
MongoDB & NoSQL 101Jollen Chen
 

Similaire à Building your First App with MongoDB (20)

OSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross LawleyOSDC 2012 | Building a first application on MongoDB by Ross Lawley
OSDC 2012 | Building a first application on MongoDB by Ross Lawley
 
Mongodb intro
Mongodb introMongodb intro
Mongodb intro
 
Building web applications with mongo db presentation
Building web applications with mongo db presentationBuilding web applications with mongo db presentation
Building web applications with mongo db presentation
 
MongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overviewMongoDB: a gentle, friendly overview
MongoDB: a gentle, friendly overview
 
Building APIs in an easy way using API Platform
Building APIs in an easy way using API PlatformBuilding APIs in an easy way using API Platform
Building APIs in an easy way using API Platform
 
MongoDB at GUL
MongoDB at GULMongoDB at GUL
MongoDB at GUL
 
Learn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDBLearn Learn how to build your mobile back-end with MongoDB
Learn Learn how to build your mobile back-end with MongoDB
 
Managing Social Content with MongoDB
Managing Social Content with MongoDBManaging Social Content with MongoDB
Managing Social Content with 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
 
10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCup10gen MongoDB Video Presentation at WebGeek DevCup
10gen MongoDB Video Presentation at WebGeek DevCup
 
Building your first app with MongoDB
Building your first app with MongoDBBuilding your first app with MongoDB
Building your first app with MongoDB
 
Webinar: What's new in the .NET Driver
Webinar: What's new in the .NET DriverWebinar: What's new in the .NET Driver
Webinar: What's new in the .NET Driver
 
MongoDB Basics
MongoDB BasicsMongoDB Basics
MongoDB Basics
 
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)
 
Dev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDBDev Jumpstart: Build Your First App with MongoDB
Dev Jumpstart: Build Your First App with MongoDB
 
Webinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev TeamsWebinar: General Technical Overview of MongoDB for Dev Teams
Webinar: General Technical Overview of MongoDB for Dev Teams
 
Building Your First MongoDB Application
Building Your First MongoDB ApplicationBuilding Your First MongoDB Application
Building Your First MongoDB Application
 
9b. Document-Oriented Databases lab
9b. Document-Oriented Databases lab9b. Document-Oriented Databases lab
9b. Document-Oriented Databases lab
 
Introduction to RavenDB
Introduction to RavenDBIntroduction to RavenDB
Introduction to RavenDB
 
MongoDB & NoSQL 101
 MongoDB & NoSQL 101 MongoDB & NoSQL 101
MongoDB & NoSQL 101
 

Plus de MongoDB

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 AtlasMongoDB
 
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
 
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
 
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 MongoDBMongoDB
 
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
 
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 DataMongoDB
 
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 StartMongoDB
 
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
 
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.2MongoDB
 
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
 
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
 
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 MindsetMongoDB
 
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 JumpstartMongoDB
 
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
 
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
 
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
 
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 DiveMongoDB
 
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 & GolangMongoDB
 
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
 
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...MongoDB
 

Plus de 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 with MongoDB

  • 1. Building your First App with MongoDB Blossom Coryat Solutions Architect, 10gen
  • 2. Today‟s Talk • Introduction to MongoDB • Discuss data modeling, queries, indexes, updates and aggregation in MongoDB within the context of building a location-based app
  • 4. MongoDB is a ___________ database • Document Oriented • Open source • High performance • Highly Available • Horizontally scalable • Full featured
  • 5. Document Oriented • A document is essentially an associative array • Document == JSON object • Document == PHP Array • Document == Python Dict • Document == Ruby Hash • Etc.
  • 6. Open Source • MongoDB is an open source project – Started and maintained by 10gen • Licensed under the AGPL • Commercial licenses available • On GitHub • Contributions welcome
  • 7. High Performance • Written in C++ • Runs nearly everywhere • Extensive use of memory-mapped files • 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 • Flexible schema • Geospatial features • Support for most programming languages
  • 9. MongoDB drivers • Official Support for 12 languages • Community drivers for tons more • Drivers connect to mongo servers • Drivers translate BSON into native types • Installed using typical means (npm, pecl, gem, pip)
  • 10.
  • 11.
  • 13. Replica Sets • Redundancy and Failover • Multi-DC deployments • Zero downtime for upgrades and maintenance
  • 15. Sharding • Partition your data • Auto-balancing • Scale write throughput • Increase capacity
  • 17. RDBMS MongoDB Row ➜ Document Table, View ➜ Collection Index ➜ Index Join ➜ Embedded Document Foreign ➜ Reference Key Partition ➜ Shard Terminology
  • 20. Example Document { _id : ObjectId("4c4ba5c0672c685e5e8aabf3"), author : "Blossom", date : ISODate("2012-11-02T11:52:27.442Z"), title: "Intro to MongoDB", tags : [ "tech", "databases”, ”documents" ], comments : [{ author : "Fred", date : ISODate("2012-11-03T17:22:21.124Z"), text : ”interesting article, lots of great nuggets of wisdom" }], comment_count : 1 }
  • 22. Requirements • Users should be able to check in to a location • Should be able to view check-ins by location • Ability to assign categories to places • Find nearby places • Generate check-in statistics
  • 23. First step in any application is Determine your entities
  • 24. Check-in App Entities • People checking in • Places to check in to • Check-in events
  • 25. In a relational based app We would start by doing schema design
  • 26. Relational schema design • Large entity relationship diagrams • Complex create table statements • Tables just to join tables together • ORMs to map tables to objects • Lots of revisions until we get it just right
  • 27. In a MongoDB based app We start building our and let the schema evolve app
  • 28. MongoDB collections • Users • Places • Check-ins – Separate collection to manage discrete check-in events and support high workload of writes
  • 29. We‟ll use the JS shell to get started
  • 30. Start with an object (or array, hash, dict, etc) > user = { firstname: 'fred', lastname: 'jones', }
  • 31. Insert the record > db.users.insert(user) No collection creation needed
  • 32. Querying for the user > db.users.findOne() { "_id" : ObjectId("50804d0bd94ccab2da652599"), "firstname" : "fred", "lastname" : "jones" }
  • 33. _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
  • 34. ObjectId • ObjectId is a special 12 byte value • Guaranteed to be unique across your cluster • ObjectId("50804d0bd94ccab2da652599") |---------||-----||-----||--------| ts mac pid inc
  • 35. Places v1 > db.places.insert({ name: “Exploratorium”, address: “3601 Lyon St” city: “San Francisco”, postcode: 94123 }
  • 36. Places v1 > db.places.insert({ name: “Exploratorium”, address: “3601 Lyon St” city: “San Francisco”, postcode: 94123 } > db.places.find({name:”Exploratorium”})
  • 37. Places v2 > db.places.insert({ name: “Exploratorium”, address: “3601 Lyon St” city: “San Francisco”, postcode: 94123, categories: [“Social”, “Arts”, “Museums”] }
  • 38. Places v2 > db.places.insert({ name: “Exploratorium”, address: “3601 Lyon St” city: “San Francisco”, postcode: 94123, categories: [“Social”, “Arts”, “Museums”] } > db.places.ensureIndex({categories: 1}) > db.places.find({categories: ”Social”})
  • 39. Places v3 > db.places.insert({ name: “Exploratorium”, address: “3601 Lyon St” city: “San Francisco”, postcode: 94123, categories: [“Social”, “Arts”, “Museums”], location: [37.803963,-122.448531] } > db.places.ensureIndex({location: “2d”}) > db.places.find({location:{$near:[37.81,-122.45]}})
  • 40. Indexes • Can be created on any field – Including fields within sub-documents or embedded arrays • Support for compound indexes on any combination of keys • Indexes should exist for primary, common, and user-facing queries
  • 41. Indexes on Places // creating indexes > db.places.ensureIndex({name:1}) > db.places.ensureIndex({categories:1}) > db.places.ensureIndex({location:”2d”}) // finding by regular expression > db.places.find({name:/^explor/i}) // finding nearby > db.places.find({location:{$near:[37.81,-122.45]}}) // finding by category > db.places.find({categories: ”Social”})
  • 42. Updating Documents • MongoDB supports atomic, in-place document updates • $inc • $set, $unset • $push, $pushAll • $addToSet, $each • $pop, $pull, $pullAll • $rename • $bit
  • 43. Updating > db.checkins.insert({ place_id: pid, user_id: uid, datetime: new Date() }) // update the “checkins” count on place and user // “checkins” will be added if it doesn‟t exist > db.places.update({_id: pid}, {$inc: {checkins: 1}}) > db.users.update({_id: uid}, {$inc: {checkins: 1}})
  • 44. Simple Statistics // find the last 10 checkins for a place: > p = db.places.find({name:”Exploratorium”}) > db.checkins.find({place_id:p[„_id‟]}) .sort({datetime:-1}).limit(10) // how many people checked in today > db.checkins.find({place_id:p[„_id‟], datetime: {$gt: midnight}}).count()
  • 45. Aggregation Framework • Documents pass through a pipeline of operations • Mechanism to calculate aggregate values across your data • Similar functionality to GROUP BY and related SQL operators • Along with computation, data can be reshaped – Add computed fields – Create virtual sub-objects – Extract sub-fields
  • 46. Aggregation Framework Operators • $project • $match • $limit • $skip • $unwind • $group • $sort
  • 47.
  • 48. Advanced Statistics // checkins per place per day of the week > db.checkins.aggregate( {$project: { place_id:1, dotw: { $dayOfWeek: ”$datetime”} }}, {$group: { _id: {place_id: “$place_id”, dotw: “$dotw”}, count: {$sum: 1} }})
  • 50. Deployment • Single server - need a strong backup plan P
  • 51. Deployment • Single server - need a strong backup plan P • Replica sets P S S - High availability - Automatic failover
  • 52. Deployment • Single server - need a strong backup plan P • Replica sets P S S - High availability - Automatic failover P S S • Sharded - Horizontally scale - Auto balancing P S S

Notes de l'éditeur

  1. AGPL – GNU Affero General Public License
  2. 10gen Drivers
  3. Plus community drivers.
  4. "50804d0bd94ccab2da652599" is a 24 byte string (12 byte ObjectId hex encoded).