Upload
mongodb
View
7.632
Download
0
Tags:
Embed Size (px)
DESCRIPTION
This webinar will introduce the features of MongoDB by walking through how one can building a simple location-based checkin application using MongoDB. The talk will cover the basics of MongoDB's document model, query language, map-reduce framework and deployment architecture.
Citation preview
Solutions Architect, 10gen
Blossom Coryat
Building your First App with MongoDB
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 MongoDBRow ➜ DocumentTable, View
➜ Collection
Index ➜ IndexJoin ➜ Embedded
DocumentForeign Key
➜ Reference
Partition ➜ ShardTerminology
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 isDetermine your entities
Check-in App Entities
• People checking in
• Places to check in to
• Check-in events
In a relational based appWe 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 appWe start building our appand let the schema evolve
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
> user = {
firstname: 'fred',
lastname: 'jones',
}
Start with an object (or array, hash, dict, etc)
> db.users.insert(user)
Insert the record
No collection creation needed
> db.users.findOne()
{
"_id" : ObjectId("50804d0bd94ccab2da652599"),
"firstname" : "fred",
"lastname" : "jones"
}
Querying for the user
_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
> 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 v1
> 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 v2
> 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]}})
Places v3
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
// 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”})
Indexes on Places
Updating Documents
• MongoDB supports atomic, in-place document updates
• $inc
• $set, $unset
• $push, $pushAll
• $addToSet, $each
• $pop, $pull, $pullAll
• $rename
• $bit
> 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}})
Updating
// 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()
Simple Statistics
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
// 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}
}})
Advanced Statistics
Deployment
P
Deployment
• Single server- need a strong backup plan
Deployment
• Single server- need a strong backup plan
• Replica sets- High availability- Automatic failover
P
P S S
• Single server- need a strong backup plan
• Replica sets- High availability- Automatic failover
• Sharded- Horizontally scale- Auto balancing
Deployment
P S S
P S S
P
P S S
10gen.com/presentations
education.10gen.com
Learn More: