Webinar: Building Your First Application with MongoDB

53
Solutions Architect, 10gen Blossom Coryat Building your First App with MongoDB

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.

Transcript of Webinar: Building Your First Application with MongoDB

Page 1: Webinar: Building Your First Application with MongoDB

Solutions Architect, 10gen

Blossom Coryat

Building your First App with MongoDB

Page 2: Webinar: Building Your First Application 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

Page 3: Webinar: Building Your First Application with MongoDB

What is MongoDB?

Page 4: Webinar: Building Your First Application with MongoDB

MongoDB is a ___________ database• Document Oriented

• Open source

• High performance

• Highly Available

• Horizontally scalable

• Full featured

Page 5: Webinar: Building Your First Application with MongoDB

Document Oriented

• A document is essentially an associative array

• Document == JSON object

• Document == PHP Array

• Document == Python Dict

• Document == Ruby Hash

• Etc.

Page 6: Webinar: Building Your First Application with MongoDB

Open Source

• MongoDB is an open source project– Started and maintained by 10gen

• Licensed under the AGPL

• Commercial licenses available

• On GitHub

• Contributions welcome

Page 7: Webinar: Building Your First Application with MongoDB

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

Page 8: Webinar: Building Your First Application with MongoDB

Full Featured

• Ad Hoc queries

• Real time aggregation

• Rich query capabilities

• Flexible schema

• Geospatial features

• Support for most programming languages

Page 9: Webinar: Building Your First Application with MongoDB

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)

Page 10: Webinar: Building Your First Application with MongoDB
Page 11: Webinar: Building Your First Application with MongoDB
Page 12: Webinar: Building Your First Application with MongoDB

Highly Available

Page 13: Webinar: Building Your First Application with MongoDB

Replica Sets

• Redundancy and Failover

• Multi-DC deployments

• Zero downtime for upgrades and maintenance

Page 14: Webinar: Building Your First Application with MongoDB

Horizontally Scalable

Page 15: Webinar: Building Your First Application with MongoDB

Sharding

• Partition your data

• Auto-balancing

• Scale write throughput

• Increase capacity

Page 16: Webinar: Building Your First Application with MongoDB

Document Database

Page 17: Webinar: Building Your First Application with MongoDB

RDBMS MongoDBRow ➜ DocumentTable, View

➜ Collection

Index ➜ IndexJoin ➜ Embedded

DocumentForeign Key

➜ Reference

Partition ➜ ShardTerminology

Page 18: Webinar: Building Your First Application with MongoDB

Typical (relational) ERD

Page 19: Webinar: Building Your First Application with MongoDB

MongoDB ERD

Page 20: Webinar: Building Your First Application with MongoDB

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

}

Page 21: Webinar: Building Your First Application with MongoDB

Building a check-in web app

Page 22: Webinar: Building Your First Application with MongoDB

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

Page 23: Webinar: Building Your First Application with MongoDB

First step in any application isDetermine your entities

Page 24: Webinar: Building Your First Application with MongoDB

Check-in App Entities

• People checking in

• Places to check in to

• Check-in events

Page 25: Webinar: Building Your First Application with MongoDB

In a relational based appWe would start by doing schema design

Page 26: Webinar: Building Your First Application with MongoDB

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

Page 27: Webinar: Building Your First Application with MongoDB

In a MongoDB based appWe start building our appand let the schema evolve

Page 28: Webinar: Building Your First Application with MongoDB

MongoDB collections

• Users

• Places

• Check-ins– Separate collection to manage discrete check-in

events and support high workload of writes

Page 29: Webinar: Building Your First Application with MongoDB

We’ll use the JS shell to get started

Page 30: Webinar: Building Your First Application with MongoDB

> user = {

firstname: 'fred',

lastname: 'jones',

}

Start with an object (or array, hash, dict, etc)

Page 31: Webinar: Building Your First Application with MongoDB

> db.users.insert(user)

Insert the record

No collection creation needed

Page 32: Webinar: Building Your First Application with MongoDB

> db.users.findOne()

{

"_id" : ObjectId("50804d0bd94ccab2da652599"),

"firstname" : "fred",

"lastname" : "jones"

}

Querying for the user

Page 33: Webinar: Building Your First Application with MongoDB

_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

Page 34: Webinar: Building Your First Application with MongoDB

ObjectId

• ObjectId is a special 12 byte value

• Guaranteed to be unique across your cluster

• ObjectId("50804d0bd94ccab2da652599") |---------||-----||-----||--------| ts mac pid inc

Page 35: Webinar: Building Your First Application with MongoDB

> db.places.insert({

name: “Exploratorium”,

address: “3601 Lyon St”

city: “San Francisco”,

postcode: 94123

}

Places v1

Page 36: Webinar: Building Your First Application with MongoDB

> db.places.insert({

name: “Exploratorium”,

address: “3601 Lyon St”

city: “San Francisco”,

postcode: 94123

}

> db.places.find({name:”Exploratorium”})

Places v1

Page 37: Webinar: Building Your First Application with MongoDB

> db.places.insert({

name: “Exploratorium”,

address: “3601 Lyon St”

city: “San Francisco”,

postcode: 94123,

categories: [“Social”, “Arts”, “Museums”]

}

Places v2

Page 38: Webinar: Building Your First Application with MongoDB

> 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

Page 39: Webinar: Building Your First Application with MongoDB

> 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

Page 40: Webinar: Building Your First Application with MongoDB

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

Page 41: Webinar: Building Your First Application with MongoDB

// 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

Page 42: Webinar: Building Your First Application with MongoDB

Updating Documents

• MongoDB supports atomic, in-place document updates

• $inc

• $set, $unset

• $push, $pushAll

• $addToSet, $each

• $pop, $pull, $pullAll

• $rename

• $bit

Page 43: Webinar: Building Your First Application with MongoDB

> 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

Page 44: Webinar: Building Your First Application with MongoDB

// 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

Page 45: Webinar: Building Your First Application with MongoDB

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

Page 46: Webinar: Building Your First Application with MongoDB

Aggregation Framework Operators

• $project

• $match

• $limit

• $skip

• $unwind

• $group

• $sort

Page 47: Webinar: Building Your First Application with MongoDB
Page 48: Webinar: Building Your First Application with MongoDB

// 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

Page 49: Webinar: Building Your First Application with MongoDB

Deployment

Page 50: Webinar: Building Your First Application with MongoDB

P

Deployment

• Single server- need a strong backup plan

Page 51: Webinar: Building Your First Application with MongoDB

Deployment

• Single server- need a strong backup plan

• Replica sets- High availability- Automatic failover

P

P S S

Page 52: Webinar: Building Your First Application with MongoDB

• 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

Page 53: Webinar: Building Your First Application with MongoDB

10gen.com/presentations

education.10gen.com

Learn More: