Download - Building Your First App with MongoDB
![Page 1: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/1.jpg)
Perl Engineer & Evangelist, 10gen
Mike Friedman
#MongoBoston
Building your first app:an introduction to MongoDB
![Page 2: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/2.jpg)
What is MongoDB?
![Page 3: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/3.jpg)
MongoDB is a ___________ database• Document
• Open source
• High performance
• Horizontally scalable
• Full featured
![Page 4: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/4.jpg)
Document Database
• Not for .PDF & .DOC files
• A document is essentially an associative array
• Document == JSON Object
• Document == Perl Hash
• Document == Python Dict
• Document == Ruby Hash
• etc.
![Page 5: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/5.jpg)
Open Source
• MongoDB is an open source project
• On GitHub
• Server licensed under AGPL
• Drivers licensed under Apache
• Started & sponsored by 10gen
• Commercial licenses available
• Contributions welcome
![Page 6: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/6.jpg)
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 = less work
![Page 7: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/7.jpg)
Horizontally Scalable
![Page 8: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/8.jpg)
Full Featured
• Ad Hoc queries
• Real time aggregation
• Rich query capabilities
• Geospatial features
• Support for most programming languages
• Flexible schema
![Page 9: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/9.jpg)
http://www.mongodb.org/downloads
![Page 10: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/10.jpg)
Mongo Shell
![Page 11: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/11.jpg)
Document Database
![Page 12: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/12.jpg)
RDBMS MongoDBTable, View ➜ CollectionRow ➜ DocumentIndex ➜ IndexJoin ➜ Embedded
DocumentForeign Key ➜ ReferencePartition ➜ Shard
Terminology
![Page 13: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/13.jpg)
Typical (relational) ERD
![Page 14: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/14.jpg)
MongoDB ERD
![Page 15: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/15.jpg)
http://www.flickr.com/photos/somegeekintn/3484353131/
We will build a librarymanagement application
![Page 16: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/16.jpg)
First step in any application isDetermine your entities
![Page 17: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/17.jpg)
Library Management Application Entities
• Library Patrons (users)
• Books (catalog)
• Authors
• Publishers
• Categories ??
![Page 18: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/18.jpg)
In a relational based appWe would start by doing schema design
![Page 19: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/19.jpg)
Relational schema design• Large ERD Diagrams
• Complex create table statements
• ORMs to map tables to objects
• Tables just to join tables together
• For this simple app we'd have 5 tables and 5 join tables
• Lots of revisions until we get it just right
![Page 20: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/20.jpg)
In a MongoDB based appWe start building our appand let the schema evolve
![Page 21: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/21.jpg)
MongoDB collections
• Users
• Books
• Authors
![Page 22: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/22.jpg)
Working with MongoDB
![Page 23: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/23.jpg)
user = {
username: 'fred.jones',
first_name: 'Fred',
last_name: 'Jones',
}
Start with an object (or array, hash, dict, etc)
![Page 24: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/24.jpg)
> db.users.insert(user)
Insert the record
No collection creation needed
![Page 25: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/25.jpg)
> db.users.findOne()
{
"_id" : ObjectId("50804d0bd94ccab2da652599"),
"username" : "fred.jones",
"first_name" : "Fred",
"last_name" : "Jones"
}
Querying for the user
![Page 26: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/26.jpg)
_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 27: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/27.jpg)
ObjectId
• ObjectId is a special 12 byte value
• Guaranteed to be unique across your cluster
• ObjectId("50804d0bd94ccab2da652599")
Timestamp machine PID Increment
![Page 28: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/28.jpg)
> 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 29: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/29.jpg)
> db.author.findOne( { last_name : 'Tolkien' } )
{
"_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."
}
Querying for our author
![Page 30: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/30.jpg)
> 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: new Date('21 July 1954'),
}
})
Creating a Book
http://society6.com/PastaSoup/The-Fellowship-of-the-Ring-ZZc_Print/
![Page 31: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/31.jpg)
> db.books.findOne({language: 'English'}, {genre: 1})
{
"_id" : ObjectId("50804391d94ccab2da652598"),
"genre" : [
"fantasy",
"adventure"
]
}
Multiple values per key
![Page 32: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/32.jpg)
> db.books.findOne({genre: 'fantasy'}, {title: 1})
{
"_id" : ObjectId("50804391d94ccab2da652598"),
"title" : "Fellowship of the Ring, The"
}
Querying for key with multiple values
Query key with single value or multiple values the same way.
![Page 33: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/33.jpg)
> db.books.findOne({}, {publication: 1})
{
"_id" : ObjectId("50804ec7d94ccab2da65259a"),
"publication" : {
"name" : "George Allen & Unwin",
"location" : "London",
"date" : ISODate("1954-07-21T04:00:00Z")
}
}
Nested Values
![Page 34: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/34.jpg)
> db.books.findOne({'publication.date' :
{ $lt : new Date('21 June 1960')}}
){
"_id" : ObjectId("50804391d94ccab2da652598"),"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-
21T04:00:00Z")}
}
Reach into nested valuesusing dot notation
![Page 35: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/35.jpg)
> db.books.update(
{"_id" : ObjectId("50804391d94ccab2da652598")},
{ $set : {
isbn: '0547928211',
pages: 432
}
})
Update books
True agile development . Simply change how you work with the data and the database follows
![Page 36: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/36.jpg)
db.books.findOne(){
"_id" : ObjectId("50804ec7d94ccab2da65259a"),"author" : ObjectId("507ffbb1d94ccab2da652597"),"genre" : [ "fantasy", "adventure" ],"isbn" : "0395082544","language" : "English","pages" : 432,"publication" : {
"name" : "George Allen & Unwin","location" : "London","date" : ISODate("1954-07-
21T04:00:00Z")},"title" : "Fellowship of the Ring, The"
}
The Updated Book record
![Page 37: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/37.jpg)
> db.books.ensureIndex({title: 1})
> db.books.ensureIndex({genre : 1})
> db.books.ensureIndex({'publication.date': -1})
Creating indexes
![Page 38: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/38.jpg)
> db.books.findOne({title : /^Fell/}){
"_id" : ObjectId("50804ec7d94ccab2da65259a"),"author" : ObjectId("507ffbb1d94ccab2da652597"),"genre" : [ "fantasy", "adventure" ],"isbn" : "0395082544","language" : "English","pages" : 432,"publication" : {
"name" : "George Allen & Unwin","location" : "London","date" : ISODate("1954-07-
21T04:00:00Z")},"title" : "Fellowship of the Ring, The"
}
Querying with RegEx
![Page 39: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/39.jpg)
> db.books.insert({ title: 'Two Towers, The',author:
ObjectId("507ffbb1d94ccab2da652597"),language: 'English',isbn : "034523510X",genre: ['fantasy', 'adventure'],pages: 447,publication: {
name: 'George Allen & Unwin',
location: 'London', date: new Date('11 Nov 1954'),
}})
Adding a few more books
http://society6.com/PastaSoup/The-Two-Towers-XTr_Print/
![Page 40: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/40.jpg)
> db.books.insert({ 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: new Date('20 Oct 1955'),
}})
Adding a few more books
http://society6.com/PastaSoup/The-Return-of-the-King-Jsc_Print/
![Page 41: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/41.jpg)
> 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-
20T04:00:00Z")}
}
Cursors
![Page 42: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/42.jpg)
page_num = 3;results_per_page = 10;cursor = db.books.find() .sort({ "publication.date" : -1 }) .skip((page_num - 1) * results_per_page) .limit(results_per_page);
Paging
![Page 43: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/43.jpg)
> 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 44: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/44.jpg)
MongoDB Drivers
![Page 45: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/45.jpg)
Real applications are not built in the shell
![Page 46: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/46.jpg)
MongoDB has native bindings for over 12 languages
![Page 47: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/47.jpg)
![Page 48: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/48.jpg)
![Page 49: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/49.jpg)
MongoDB drivers
• Official Support for 12 languages
• Community drivers for tons more
• Drivers connect to MongoDB servers
• Drivers translate BSON into native types
• MongoDB shell is not a driver, but works like one in some ways
• Installed using typical means (npm, cpan, gem, pip)
![Page 50: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/50.jpg)
Next Steps
![Page 51: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/51.jpg)
We've introduced a lot of concepts here
![Page 52: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/52.jpg)
Schema Design @ 10:45 am
![Page 53: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/53.jpg)
Indexing/Query Optimization @ 11:40 am
![Page 54: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/54.jpg)
Replication @ 1:55 pm
![Page 55: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/55.jpg)
Sharding @ 2:40 pm
![Page 56: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/56.jpg)
Aggregation @ 4:25 pm
$project $unwind $group
![Page 57: Building Your First App with MongoDB](https://reader036.vdocuments.us/reader036/viewer/2022081515/55515a89b4c905a8768b4bec/html5/thumbnails/57.jpg)
Perl Engineer & Evangelist, 10gen
Mike Friedman
#MongoBoston
Questions?
@ 10:45 am@ 11:40 am@ 1:55 pm@ 2:40 pm@ 4:25 pm