#mongodb 
Build your first app; 
an introduction to MongoDB 
Christian Amor Kvalheim 
Developer Experience Team Lead, MongoDB
Christian Kvalheim 
• Team Lead MongoDB Drivers 
• Node.js driver developer 
• Been using MongoDB for 5 years 
• Working at MongoDB last 3 years
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 
• etc
Open Source 
• MongoDB is an open source project 
• On GitHub 
• Licensed under the AGPL 
• Started & sponsored by MongoDB Inc 
• 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 = agile development
Database Landscape
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
mongodb.org/downloads
Running 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
Mongo Shell 
$ mongo 
MongoDB shell version: 2.6.4 
connecting to: test 
> db.test.insert({text: 'Welcome to MongoDB'}) 
> db.test.find().pretty() 
{ 
"_id" : ObjectId("51c34130fbd5d7261b4cdb55"), 
"text" : "Welcome to MongoDB" 
}
Building an App with 
MongoDB
Terminology 
RDBMS MongoDB 
Table, View ➜ Collection 
Row ➜ Document 
Index ➜ Index 
Join ➜ Embedded Document 
Foreign Key ➜ Reference 
Partition ➜ Shard
Let’s Build a Monitoring System
First step in any application is 
Determine basic requirements
Basic Monitoring Requirements 
• Data generated at per-second intervals 
• Metrics to capture (ex. CPU, memory, IO) 
• Document structure for fast writes and easy reads
In a relational base app 
We would start by doing schema 
design
Relational Schema Options 
CPU Metrics 
Memory Metrics 
IO Metrics 
Events 
• Timestamp 
• CPU metrics 
• Memory metrics 
• IO metrics 
Or
In a MongoDB based app 
We start with a generic model 
and let the documents evolve
MongoDB Document Modeling 
Events 
Timestamp (min) 
CPU [] 
• Second 
• Metric 
Memory [] 
• Second 
• Metric 
IO [] 
• Second 
• Metric
Working With MongoDB
Start with the Mongo Shell 
$ mongo 
MongoDB shell version: 2.6.4 
connecting to: test 
> 
Full Javascript shell
Switch to Your DB 
> use monitoring 
switching to db monitoring 
DB files are created once you save a 
document
Create an Empty Event 
> var event = { 
ts: ISODate(“2014-09-16T09:00:00”), 
cpu: [ 0, 0, …, 0 ], 
memory: [ 0, 0, …, 0 ], 
io: [ 0, 0, …, 0 ], 
} 
Insert zeroed-out arrays to fill in later
Insert the Event 
> db.events.insert(event) 
No collection creation necessary
Find One Record 
> 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 ] 
}
How do you capture events? 
Serve 
r 
Serve 
r 
Serve 
r 
MongoD 
B 
memory 
interaction 
interaction 
interaction
Adding a New Event 
> db.events.update( 
{ ts: ISODate(“2014-09-16T09:00:00”) } 
{ $set: { 
“cpu.0” : 50, 
“memory.0”: 1500, 
“io.0”: 10 } 
} 
) 
Use atomic in-place updates to add metric values
How do you plot charts?
Finding an Hour of Events 
> db.events.find({ 
ts: { 
$gte: ISODate(“2014-09-16T09:00:00”), 
$lt: ISODate(“2014-09-16T10:00:00”) 
} 
}).sort({ts:1}) 
Find using a range and sort results 
ascending
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
Aggregate Metrics 
> 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” } } } 
]) 
Use Aggregation Framework to roll up data
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
MongoDB Drivers
Real applications are not 
built in the shell
MongoDB has native 
bindings for over 12 
languages
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)
docs.mongodb.org
Online Training at MongoDB 
University
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
Questions?
#mongodb 
Thank You 
Christian Amor Kvalheim 
Developer Experience Team Lead, MongoDB

Dev Jumpstart: Build Your First App with MongoDB

  • 1.
    #mongodb Build yourfirst app; an introduction to MongoDB Christian Amor Kvalheim Developer Experience Team Lead, MongoDB
  • 2.
    Christian Kvalheim •Team Lead MongoDB Drivers • Node.js driver developer • Been using MongoDB for 5 years • Working at MongoDB last 3 years
  • 3.
  • 4.
    MongoDB is a___________ database • Document • Open source • High performance • Horizontally scalable • Full featured
  • 5.
    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
  • 6.
    Open Source •MongoDB is an open source project • On GitHub • Licensed under the AGPL • Started & sponsored by MongoDB Inc • Commercial licenses available • Contributions welcome
  • 7.
    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
  • 8.
  • 9.
    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
  • 10.
  • 11.
    Running 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
  • 12.
    Mongo Shell $mongo MongoDB shell version: 2.6.4 connecting to: test > db.test.insert({text: 'Welcome to MongoDB'}) > db.test.find().pretty() { "_id" : ObjectId("51c34130fbd5d7261b4cdb55"), "text" : "Welcome to MongoDB" }
  • 13.
    Building an Appwith MongoDB
  • 14.
    Terminology RDBMS MongoDB Table, View ➜ Collection Row ➜ Document Index ➜ Index Join ➜ Embedded Document Foreign Key ➜ Reference Partition ➜ Shard
  • 15.
    Let’s Build aMonitoring System
  • 16.
    First step inany application is Determine basic requirements
  • 17.
    Basic Monitoring Requirements • Data generated at per-second intervals • Metrics to capture (ex. CPU, memory, IO) • Document structure for fast writes and easy reads
  • 18.
    In a relationalbase app We would start by doing schema design
  • 19.
    Relational Schema Options CPU Metrics Memory Metrics IO Metrics Events • Timestamp • CPU metrics • Memory metrics • IO metrics Or
  • 20.
    In a MongoDBbased app We start with a generic model and let the documents evolve
  • 21.
    MongoDB Document Modeling Events Timestamp (min) CPU [] • Second • Metric Memory [] • Second • Metric IO [] • Second • Metric
  • 22.
  • 23.
    Start with theMongo Shell $ mongo MongoDB shell version: 2.6.4 connecting to: test > Full Javascript shell
  • 24.
    Switch to YourDB > use monitoring switching to db monitoring DB files are created once you save a document
  • 25.
    Create an EmptyEvent > var event = { ts: ISODate(“2014-09-16T09:00:00”), cpu: [ 0, 0, …, 0 ], memory: [ 0, 0, …, 0 ], io: [ 0, 0, …, 0 ], } Insert zeroed-out arrays to fill in later
  • 26.
    Insert the Event > db.events.insert(event) No collection creation necessary
  • 27.
    Find One Record > 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 ] }
  • 28.
    How do youcapture events? Serve r Serve r Serve r MongoD B memory interaction interaction interaction
  • 29.
    Adding a NewEvent > db.events.update( { ts: ISODate(“2014-09-16T09:00:00”) } { $set: { “cpu.0” : 50, “memory.0”: 1500, “io.0”: 10 } } ) Use atomic in-place updates to add metric values
  • 30.
    How do youplot charts?
  • 31.
    Finding an Hourof Events > db.events.find({ ts: { $gte: ISODate(“2014-09-16T09:00:00”), $lt: ISODate(“2014-09-16T10:00:00”) } }).sort({ts:1}) Find using a range and sort results ascending
  • 32.
    How do youroll up the data? Hour Avg Cpu 0 50 1 65 2 75 3 40 4 45 5 60 6 25 … … 23 30
  • 33.
    Aggregate Metrics >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” } } } ]) Use Aggregation Framework to roll up data
  • 34.
    How do youscale 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
  • 35.
  • 36.
    Real applications arenot built in the shell
  • 37.
    MongoDB has native bindings for over 12 languages
  • 40.
    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)
  • 41.
  • 42.
    Online Training atMongoDB University
  • 43.
    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
  • 44.
  • 45.
    #mongodb Thank You Christian Amor Kvalheim Developer Experience Team Lead, MongoDB