Transcript
Page 1: Dev Jumpstart: Build Your First App with MongoDB

Developer Experience Team Lead, MongoDB

Christian Amor Kvalheim

#mongodb

Build your first app;an introduction to MongoDB

Page 2: Dev Jumpstart: Build Your First App with MongoDB

Christian Kvalheim• Team Lead MongoDB Drivers

• Node.js driver developer

• Been using MongoDB for 5 years

• Working at MongoDB last 3 years

Page 3: Dev Jumpstart: Build Your First App with MongoDB

What is MongoDB?

Page 4: Dev Jumpstart: Build Your First App with MongoDB

MongoDB is a ___________ database

• Document

• Open source

• High performance

• Horizontally scalable

• Full featured

Page 5: Dev Jumpstart: Build Your First App with MongoDB

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

• etc

Page 6: Dev Jumpstart: Build Your First App with MongoDB

Open Source

• MongoDB is an open source project

• On GitHub

• Licensed under the AGPL

• Started & sponsored by MongoDB Inc

• Commercial licenses available

• Contributions welcome

Page 7: Dev Jumpstart: Build Your First App with MongoDB

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 = agile development

Page 8: Dev Jumpstart: Build Your First App with MongoDB

Database Landscape

Page 9: Dev Jumpstart: Build Your First App with MongoDB

Full Featured

• Flexible schema

• Rich ad-hoc queries

• Real time aggregation

• Strongly consistent

• Geospatial features

• Built-in automatic failover

• Horizontally scalable reads/writes

• Support for most programming languages

Page 10: Dev Jumpstart: Build Your First App with MongoDB

mongodb.org/downloads

Page 11: Dev Jumpstart: Build Your First App with MongoDB

$ tar zxf mongodb-osx-x86_64-2.6.4.tgz

$ cd mongodb-osx-x86_64-2.6.4/bin

$ mkdir –p /data/db

$ ./mongod

Running MongoDB

Page 12: Dev Jumpstart: Build Your First App with MongoDB

$ mongoMongoDB shell version: 2.6.4connecting to: test> db.test.insert({text: 'Welcome to MongoDB'})> db.test.find().pretty(){

"_id" : ObjectId("51c34130fbd5d7261b4cdb55"),"text" : "Welcome to MongoDB"

}

Mongo Shell

Page 13: Dev Jumpstart: Build Your First App with MongoDB

Building an App with MongoDB

Page 14: Dev Jumpstart: Build Your First App with MongoDB

Terminology

RDBMS MongoDB

Table, View ➜ Collection

Row ➜ Document

Index ➜ Index

Join ➜ Embedded Document

Foreign Key ➜ Reference

Partition ➜ Shard

Page 15: Dev Jumpstart: Build Your First App with MongoDB

Let’s Build a Monitoring System

Page 16: Dev Jumpstart: Build Your First App with MongoDB

First step in any application is

Determine basic requirements

Page 17: Dev Jumpstart: Build Your First App with MongoDB

Basic Monitoring Requirements

• Data generated at per-second intervals

• Metrics to capture (ex. CPU, memory, IO)

• Document structure for fast writes and easy reads

Page 18: Dev Jumpstart: Build Your First App with MongoDB

In a relational base app

We would start by doing schema design

Page 19: Dev Jumpstart: Build Your First App with MongoDB

Relational Schema Options

CPU Metrics

Memory Metrics

IO Metrics

Events

• Timestamp• CPU metrics• Memory metrics• IO metrics

Or

Page 20: Dev Jumpstart: Build Your First App with MongoDB

In a MongoDB based appWe start with a generic modeland let the documents evolve

Page 21: Dev Jumpstart: Build Your First App with MongoDB

MongoDB Document Modeling

Events

CPU []• Second• Metric

Memory []• Second• Metric

IO []• Second• Metric

Timestamp (min)

Page 22: Dev Jumpstart: Build Your First App with MongoDB

Working With MongoDB

Page 23: Dev Jumpstart: Build Your First App with MongoDB

$ mongo

MongoDB shell version: 2.6.4

connecting to: test

>

Start with the Mongo Shell

Full Javascript shell

Page 24: Dev Jumpstart: Build Your First App with MongoDB

> use monitoring

switching to db monitoring

Switch to Your DB

DB files are created once you save a document

Page 25: Dev Jumpstart: Build Your First App with MongoDB

> var event = {

ts: ISODate(“2014-09-16T09:00:00”),

cpu: [ 0, 0, …, 0 ],

memory: [ 0, 0, …, 0 ],

io: [ 0, 0, …, 0 ],

}

Create an Empty Event

Insert zeroed-out arrays to fill in later

Page 26: Dev Jumpstart: Build Your First App with MongoDB

> db.events.insert(event)

Insert the Event

No collection creation necessary

Page 27: Dev Jumpstart: Build Your First App with MongoDB

> db.events.findOne()

{

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

”ts" : ISODate(“2014-09-16T09:00:00”),

”cpu" : [ 0, 0, …, 0 ],

”memory" : [ 0, 0, …, 0 ],

“io” : [ 0, 0, …, 0 ]

}

Find One Record

Page 28: Dev Jumpstart: Build Your First App with MongoDB

How do you capture events?

Server

Server

Server

MongoDB

cpu

memory

io

interaction

interaction

interaction

Page 29: Dev Jumpstart: Build Your First App with MongoDB

> db.events.update( { ts: ISODate(“2014-09-16T09:00:00”) } { $set: { “cpu.0” : 50, “memory.0”: 1500, “io.0”: 10 } })

Adding a New Event

Use atomic in-place updates to add metric values

Page 30: Dev Jumpstart: Build Your First App with MongoDB

How do you plot charts?

Page 31: Dev Jumpstart: Build Your First App with MongoDB

> db.events.find({

ts: {

$gte: ISODate(“2014-09-16T09:00:00”),

$lt: ISODate(“2014-09-16T10:00:00”)

}

}).sort({ts:1})

Finding an Hour of Events

Find using a range and sort results ascending

Page 32: Dev Jumpstart: Build Your First App with MongoDB

How do you roll up the data?

Hour Avg Cpu

0 50

1 65

2 75

3 40

4 45

5 60

6 25

… …

23 30

Page 33: Dev Jumpstart: Build Your First App with MongoDB

> db.events.aggregate([

{ $match: { ts: { $gte: date0, $lt: date1 } } },

{ $project: { _id: 0, ts: 1, cpu: 1 } },

{ $unwind: “$cpu” },

{ $group: {

_id: { $hour: “$ts” },

avg_cpu: { $avg: “$cpu” } } }

])

Aggregate Metrics

Use Aggregation Framework to roll up data

Page 34: Dev Jumpstart: Build Your First App with MongoDB

How do you scale this workload

• Replica Sets– Add data redundancy– Automatic failover– Tunable durability, consistency

• Sharding– Scale reads and writes– Support dynamic data growth– Automatically partitions workload– Horizontally scalable

Page 35: Dev Jumpstart: Build Your First App with MongoDB

MongoDB Drivers

Page 36: Dev Jumpstart: Build Your First App with MongoDB

Real applications are not built in the shell

Page 37: Dev Jumpstart: Build Your First App with MongoDB

MongoDB has native bindings for over 12 languages

Page 38: Dev Jumpstart: Build Your First App with MongoDB
Page 39: Dev Jumpstart: Build Your First App with MongoDB
Page 40: Dev Jumpstart: Build Your First App with MongoDB

MongoDB Drivers

• Official Support for 12 languages

• Community drivers for tons more

• Drivers connect to MongoDB servers

• Drivers translate BSON into native types

• Shell is not a driver, but works like one in some ways

• Installed using typical means (npm, pecl, gem, pip)

Page 41: Dev Jumpstart: Build Your First App with MongoDB

docs.mongodb.org

Page 42: Dev Jumpstart: Build Your First App with MongoDB

Online Training at MongoDB University

Page 43: Dev Jumpstart: Build Your First App with MongoDB

Suggestions for Talks

• Talk to me at Meet the Experts– 11 am – 12 pm– 1 pm – 2 pm– Or catch me in the hallway

• MongoDB for the internet of things

• Socialite the Open Source Status Feed

• The Future of MongoDB Storage

Page 44: Dev Jumpstart: Build Your First App with MongoDB

Questions?

Page 45: Dev Jumpstart: Build Your First App with MongoDB

Developer Experience Team Lead, MongoDB

Christian Amor Kvalheim

#mongodb

Thank You