building your first app: an introduction to mongodb
DESCRIPTION
Great Wide Open - Day 1 Robert Stam - MongoDB 2:30 PM - Operations 1 (Databases)TRANSCRIPT
![Page 1: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/1.jpg)
Building Your First App: An
Introduction to MongoDB
Robert StamSoftware Engineer
MongoDB, Inc.
Great Wide Open Conference
Atlanta, April 2, 2014
![Page 2: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/2.jpg)
What is MongoDB?
![Page 3: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/3.jpg)
3
• Document
• Open source
• High performance
• Horizontally scalable
• Full featured
MongoDB is a __________ database
![Page 4: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/4.jpg)
4
• Not for .PDF & .DOC files
• A document is essentially an associative array
• Document == JSON object
• Document == PHP Array
• Document == Python Dict
• Document == Ruby Hash
• Document == C# BsonDocument object model
• etc
Document Database
![Page 5: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/5.jpg)
5
• MongoDB is an open source project
• On GitHub
• Licensed under the AGPL
• Started & sponsored by MongoDB, Inc
• Commercial licenses available
• Contributions welcome
Open Source
![Page 6: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/6.jpg)
Database Landscape
![Page 7: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/7.jpg)
7
• Written in C++
• Runs nearly everywhere
• Extensive use of memory-mapped files
i.e. read-through write-through memory caching.
• Data serialized as BSON (fast parsing)
• Full support for primary & secondary indexes
• Document model = less work
High Performance
![Page 8: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/8.jpg)
8
• Ad Hoc queries
• Rich query capabilities
• Geospatial features
• Real time aggregation
• Eventually consistent
• Support for many programming languages
• Flexible schema
Full Featured
![Page 9: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/9.jpg)
9
Indexing @
![Page 10: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/10.jpg)
10
Replication @
![Page 11: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/11.jpg)
11
Sharding @
![Page 12: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/12.jpg)
mongodb.org/downloads
![Page 13: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/13.jpg)
Document Database
![Page 14: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/14.jpg)
RDBMS MongoDB
Table, View Collection
Row Document
Index Index
Join Embedded Document
Foreign Key Reference
Partition Shard
Terminology
![Page 15: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/15.jpg)
Typical (relational) ERD
![Page 16: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/16.jpg)
MongoDB ERD
![Page 17: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/17.jpg)
We will build a library management
application http://www.flickr.com/photos/somegeekintn/3484353131/
![Page 18: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/18.jpg)
First step in any application
Determine your entities
![Page 19: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/19.jpg)
19
• Library Patrons (users)
• Books (card catalog)
• Authors
• Publishers
Library Management Application
Entities
![Page 20: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/20.jpg)
In a relational based app
We would start by doing schema
design
![Page 21: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/21.jpg)
21
• Users
• Books
• Authors
• Publishers
MongoDB Collections
![Page 22: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/22.jpg)
Working with MongoDB
![Page 23: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/23.jpg)
No common programming language
so we are using the MongoDB shell
![Page 24: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/24.jpg)
24
> var user = {
username : "fred.jones",
first_name : "fred",
last_name : "jones"
}
Start with an Object(or array, hash, dict, etc)
![Page 25: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/25.jpg)
25
> use library
> db.users.insert(user)
Insert the Record
No database or collection creation needed
![Page 26: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/26.jpg)
26
> db.users.findOne()
{
_id : ObjectId("50804d0bd94ccab2da652599"),
username : "fred.jones",
first_name : "fred",
last_name : "jones"
}
Querying for the user
![Page 27: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/27.jpg)
27
• _id is the primary key in MongoDB
• Automatically created as an ObjectId if not
provided
• Any unique immutable value could be used
• Automatically indexed
_id
![Page 28: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/28.jpg)
28
• ObjectId is a special 12 byte value
• Guaranteed to be unique across your cluster
• ObjectId("50804d0bd94ccab2da652599")
ObjectId
Timestamp Machine Pid Increment
50804d0b d94cca b2da 652599
![Page 29: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/29.jpg)
29
> db.author.insert({
first_name : "j.r.r.",
last_name : "tolkien",
bio : "J.R.R. Tolkien (1892.1973), beloved
throughout the world as the creator of The Hobbit and
The Lord of the Rings, was a professor of Anglo-Saxon
at Oxford, a fellow of Pembroke College, and a fellow
of Merton College until his retirement in 1959. His
chief interest was the linguistic aspects of the early
English written tradition, but even as he studied
these classics he was creating a set of his own."
})
Creating an Author
![Page 30: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/30.jpg)
30
> db.books.insert({
title : "fellowship of the ring, the",
author : ObjectID("507ffbb1d94ccab2da652597"),
language : "english",
genre : [ "fantasy", "adventure" ],
publication : {
name : "george allen & unwin",
location : "London",
date : ISODate("1954-07-21T00:00:00Z")
}
})
Creating a Book
![Page 31: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/31.jpg)
31
> db.books.findOne(
{ language : "english" },
{ genre : 1 })
{
"_id" : ObjectID("50804391d94ccab2da652598"),
"genre" : [
"fantasy",
"adventure"
]
}
Multiple Values per Key
![Page 32: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/32.jpg)
32
> db.books.findOne(
{ genre : "fantasy" },
{ title : 1 })
{
"_id" : ObjectID("50804391d94ccab2da652598"),
"title" : "fellowship of the ring, the"
}
Querying for key with
multiple values
![Page 33: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/33.jpg)
33
> db.books.findOne({}, { publication : 1 })
{
"_id" : ObjectID("50804391d94ccab2da652598"),
"publication" : {
"name" : "george allen & unwin",
"location" : "London",
"date" : ISODate("1954-07-21T00:00:00Z")
}
}
Nested Values
![Page 34: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/34.jpg)
34
> db.books.update(
{ _id : ObjectID("50804391d94ccab2da652598") },
{ $set : {
isbn : "0547928211",
pages : 432
}}
)
True agile development. Simply Change how you work with the
data and the database follows
Update Books
![Page 35: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/35.jpg)
35
> db.books.findOne()
{
"_id" : ObjectId("50804391d94ccab2da652598"),
"author" : ObjectId("507ffbb1d94ccab2da652597"),
"genre" : [ "fantasy", "adventure" ],
"isbn" : "0547928211",
"language" : "english",
"pages" : 432,
"publication" : {
"name" : "george allen & unwin",
"location" : "London",
"date" : ISODate("1954-07-21T00:00:00Z")
},
"title" : "fellowship of the ring, the"
}
Update Books
![Page 36: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/36.jpg)
36
> db.books.ensureIndex({ title : 1 })
> db.books.ensureIndex({ genre : 1 }) // multikey
> db.books.ensureIndex({ "publication.date" : 1 })
Creating Indexes
![Page 37: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/37.jpg)
37
> db.books.findOne({ title : /^fell/ })
{
"_id" : ObjectId("50804391d94ccab2da652598"),
"author" : ObjectId("507ffbb1d94ccab2da652597"),
"genre" : [ "fantasy", "adventure" ],
"isbn" : "0395082544",
"language" : "english",
"pages" : 432,
"publication" : {
"name" : "george allen & unwin",
"location" : "London",
"date" : ISODate("1954-07-21T00:00:00Z")
},
"title" : "fellowship of the ring, the"
}
Querying with RegEx
![Page 38: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/38.jpg)
38
> db.books.insert({
title : "two towers, the",
author : ObjectID("507ffbb1d94ccab2da652597"),
language : "english",
isbn : "034523510X",
genre : [ "fantasy", "adventure" ],
page : 447,
publication : {
name : "george allen & unwin",
location : "London",
date : ISODate("1954-11-11T00:00:00Z")
}
})
Adding a Few More Books
![Page 39: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/39.jpg)
39
> db.books.find(
{ author : ObjectId("507ffbb1d94ccab2da652597") })
.sort({ "publication.date" : -1 })
.limit(1)
{
"_id" : ObjectId("5080d33ed94ccab2da65259d"),
"title" : "return of the king, the",
"author" : ObjectId("507ffbb1d94ccab2da652597"),
"language" : "english",
"isbn" : "0345248295",
"genre" : [ "fantasy", "adventure" ],
"pages" : 544,
"publication" : {
"name" : "george allen & unwin",
"location" : "London",
"date" : ISODate("1955-10-20T00:00:00Z")
}
}
Cursors
![Page 40: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/40.jpg)
40
> var page_num = 3;
> var results_per_page = 10;
> var cursor = db.books.find()
.sort({ publication_date : -1 })
.skip((page_num – 1) * results_per_page)
.limit(results_per_page);
Paging
![Page 41: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/41.jpg)
41
> var book = db.books.findOne(
{ title : "return of the king, the" })
> db.author.findOne({ _id : book.author })
{
"_id" : ObjectId("507ffbb1d94ccab2da652597"),
"first_name" : "j.r.r.",
"last_name" : "tolkien",
"bio" : "J.R.R. Tolkien (1892.1973), beloved throughout
the world as the creator of The Hobbit and The Lord of the
Rings, was a professor of Anglo-Saxon at Oxford, a fellow of
Pembroke College, and a fellow of Merton College until his
retirement in 1959. His chief interest was the linguistic
aspects of the early English written tradition, but even as
he studied these classics he was creating a set of his own."
}
Finding Author by Book
![Page 42: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/42.jpg)
MongoDB Drivers
![Page 43: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/43.jpg)
Real Applications are not built in
the shell
![Page 44: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/44.jpg)
MongoDB has native bindings
for over 12 languages
![Page 45: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/45.jpg)
![Page 46: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/46.jpg)
![Page 47: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/47.jpg)
47
• Official Support for 12 languages
• Community drivers for many more
• Drivers connect applications to mongo servers
• Drivers translate BSON into native types
• mongo shell is not a driver, but works like one in some ways
• Installed using typical means (npm, pecl, gem, pip, nuget)
MongoDB Drivers
![Page 48: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/48.jpg)
48
docs.mongodb.org
![Page 49: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/49.jpg)
49
Online Training at MongoDB University
![Page 50: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/50.jpg)
Questions?
![Page 51: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/51.jpg)
MongoDB WorldNew York City, June 23-25
Save 25% with Drivers25
Register at world.mongodb.com
See how Cisco, Stripe, Carfax, Expedia and
others are engineering the next generation of
data with MongoDB
![Page 52: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/52.jpg)
MongoDB WorldNew York City, June 23-25
See what’s next in MongoDB including
• MongoDB 2.6
• Sharding
• Replication
• Aggregation
Save 25% with Drivers25
world.mongodb.com
![Page 53: Building Your First App: An Introduction to MongoDB](https://reader034.vdocuments.us/reader034/viewer/2022051613/54c6656c4a79592d268b4579/html5/thumbnails/53.jpg)
Software Engineer, MongoDB Inc.
Robert Stam
Thank You