dev jumpstart: build your first app with mongodb
DESCRIPTION
New to MongoDB? This talk will introduce the philosophy and features of MongoDB. We’ll discuss the benefits of the document-based data model that MongoDB offers by walking through how one can build a simple app to store books. We’ll cover inserting, updating, and querying the database of books. This session will jumpstart your knowledge of MongoDB development, providing you with context for the rest of the day's content.TRANSCRIPT
![Page 1: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/1.jpg)
![Page 2: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/2.jpg)
SA, MongoDB
Norberto Leite
#mongodbdays @mongodb @nleite #developers
Building your first appAn introduction to MongoDB
![Page 3: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/3.jpg)
First Things First!
![Page 4: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/4.jpg)
Let’s not talk about Fußball!
![Page 6: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/6.jpg)
![Page 7: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/7.jpg)
Welcome to MongoDB Days Munich!
This is YOUR conference!
![Page 8: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/8.jpg)
Thanks for being part of the Family!
![Page 9: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/9.jpg)
Grab our staff for anything you need!
![Page 10: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/10.jpg)
What is MongoDB?
![Page 11: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/11.jpg)
MongoDB is a ___________ database
• Document
• Open source
• High performance
• Horizontally scalable
• Full featured
![Page 12: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/12.jpg)
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 13: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/13.jpg)
Open Source
• MongoDB is an open source project
• On GitHub
• Licensed under the AGPL
• Started & sponsored by MongoDB Inc (formerly 10gen)
• Commercial licenses available
• Contributions welcome
![Page 14: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/14.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 15: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/15.jpg)
![Page 16: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/16.jpg)
Database Landscape
![Page 17: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/17.jpg)
Full Featured
• Ad Hoc queries
• Real time aggregation
• Rich query capabilities
• Strongly consistent
• Geospatial features
• Support for most programming languages
• Flexible schema
![Page 18: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/18.jpg)
Setting Expectations
• What is MongoDB
• How to develop with MongoDB
• Scale with MongoDB
• Analytics
• MMS
• Sharding
• Setting the correct environment
![Page 19: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/19.jpg)
Ready to become a Pro!
![Page 20: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/20.jpg)
mongodb.org/downloads
![Page 21: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/21.jpg)
$ tar –z xvf mongodb-osx-x86_64-2.6.5.tgz
$ cd mongodb-osx-i386-2.4.4/bin
$ mkdir –p /data/db
$ ./mongod
Running MongoDB
![Page 22: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/22.jpg)
MacBook-Air-:~ $ mongoMongoDB shell version: 2.6.5connecting to: test> db.test.insert({text: 'Welcome to MongoDB'})> db.test.find().pretty(){
"_id" : ObjectId("51c34130fbd5d7261b4cdb55"),"text" : "Welcome to MongoDB"
}
Mongo Shell
![Page 23: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/23.jpg)
Document Database
![Page 24: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/24.jpg)
Terminology
RDBMS MongoDB
Table, View ➜ Collection
Row ➜ Document
Index ➜ Index
Join ➜ Embedded Document
Foreign Key ➜ Reference
Partition ➜ Shard
![Page 25: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/25.jpg)
Let’s Build a Blog
![Page 26: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/26.jpg)
Let’s Build a Blog
![Page 27: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/27.jpg)
Let’s Build a Personal Data Hub!
![Page 28: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/28.jpg)
First step in any application is
Determine your entities
![Page 29: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/29.jpg)
Entities in our Data Hub
• Accounts
• Messages– emails– tweets– comments– streams
• Notifications
![Page 30: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/30.jpg)
In a relational base app
We would start by doing schema design
![Page 31: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/31.jpg)
Typical (relational) ERD
Messages
Tweets
messages
Accounts
Alerts
![Page 32: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/32.jpg)
![Page 33: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/33.jpg)
Das ist beängstigende Sache
![Page 34: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/34.jpg)
In a MongoDB based appWe start building our appand let the schema evolve
![Page 35: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/35.jpg)
MongoDB ERD
Accounts
Alerts
- account- user- password- refresh_rate- uri
Messages
- text- user- time- retweets
- from- to- body- attachments
- id- time- account_id
- subscribers- channel- rate- period- metrics:[]
…
![Page 36: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/36.jpg)
Working With MongoDB
![Page 37: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/37.jpg)
Demo time
![Page 38: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/38.jpg)
>db
test
> use datahub
switching to db datahub
Switch to Your DB
![Page 39: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/39.jpg)
>var account = {
"name": "gmail",
"credentials": {
"user": "[email protected]",
"password": "YOU WISH!"
},
"smtp": "smpt.gmail.com",
"tls": true
}
Create our first Document
![Page 40: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/40.jpg)
>db
test
> use datahub
switching to db datahub
> db.accounts.insert( account )
Switch to Your DB
![Page 41: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/41.jpg)
> db.accounts.insert(account)
Insert the Record
No collection creation necessary
![Page 42: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/42.jpg)
> db.accounts.findOne()
{
"_id": ObjectId("54490561150027cc775b1019"),
"name": "gmail",
"credentials": {
"user": "[email protected]",
"password": "YOU WISH!"
},
"smtp": "smpt.gmail.com",
"tls": true
}
Find One Record
![Page 43: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/43.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 44: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/44.jpg)
ObjectId
• ObjectId is a special 12 byte value
• Guaranteed to be unique across your cluster
• ObjectId("50804d0bd94ccab2da652599") |----ts-----||---mac---||-pid-||----inc-----| 4 3 2 3
![Page 45: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/45.jpg)
> db.accounts.findOne()
{
"_id": ObjectId("54490561150027cc775b1019"),
"name": "gmail",
"credentials": {
"user": "[email protected]",
"password": "YOU WISH!"
},
”last_access": ISODate("2014-10-30T13:09:36.724Z"),
"smtp": "smpt.gmail.com",
"tls": true
}
Rich Data Types
Strings
Date
Boolean
![Page 46: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/46.jpg)
BSON
![Page 47: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/47.jpg)
> db.messages.insert({
"_id" : ObjectId("54527e08257844421e64623f"),
"favorited" : false,
"contributors" : null,
"truncated" : false,
"text" : "converting to #java 8",
"in_reply_to_status_id" : null,
”hashtags”: [ "#java", ]
…
}
Inserting Messages (emails, tweets …)
![Page 48: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/48.jpg)
> db.messages.insert({
"_id" : ObjectId("54523d2d25784427c6fabce1"),
"From" : "[email protected]",
"To" : "[email protected]",
"Date" : ISODate("2012-08-15T22:32:34Z"),
"body" : {
"text/plain" : ”Hello Munich, nice to see yalll!"
},
"Subject" : ”Live From MongoDB World"
})
Inserting Messages (emails, tweets …)
![Page 49: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/49.jpg)
> db.message.find().pretty(){
"_id" : ObjectId("54523d2d25784427c6fabce1"),
"From" : "[email protected]",
"To" : "[email protected]",
"Date" : ISODate("2012-08-15T22:32:34Z"),
"body" : {
"text/plain" : ”Hello Munich, nice to see yalll!"
},
"Subject" : ”Live From MongoDB World"
}
{
"_id" : ObjectId("54527e08257844421e64623f"),
"favorited" : false,
"contributors" : null,
"truncated" : false,
"text" : "converting to #java 8",
"in_reply_to_status_id" : null,
”hashtags”: [ "#java", ]
…
}
Finding a Message
![Page 50: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/50.jpg)
> db.article.find({"hashtags":"#java"}).pretty(){
"_id" : ObjectId("54527e08257844421e64623f"),
"favorited" : false,
"contributors" : null,
"truncated" : false,
"text" : "converting to #java 8, #programing ",
"in_reply_to_status_id" : null,
”hashtags”: [ "#java", "#programing"]
…
}
Querying An Array
query in JSON
![Page 51: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/51.jpg)
> db.messages.update({
"_id" : ObjectId("54523d2d25784427c6fabce1") },
{$set: { opened:
{date: ISODate("2012-08-15T22:32:34Z"), user:
’Norberto'}
}
})>
Using Update to Add a Comment
set new field on the document
which is a subdocument
![Page 52: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/52.jpg)
> db.message.findOne({"_id" : ObjectId("54523d2d25784427c6fabce1")})
{
"_id" : ObjectId("54523d2d25784427c6fabce1"),
"From" : "[email protected]",
"To" : "[email protected]",
"Date" : ISODate("2012-08-15T22:32:34Z"),
"body" : {
"text/plain" : ”Hello Munich, nice to see yalll!"
},
"Subject" : ”Live From MongoDB World”
"opened" : {"date": ISODate("2012-08-15T22:32:34Z"), "user": ’Norberto'}
}
Post with Comment Attached
Find document by primary key
![Page 53: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/53.jpg)
MongoDB Drivers
![Page 54: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/54.jpg)
Real applications are not built in the shell
![Page 55: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/55.jpg)
MongoDB has native bindings for over 12 languages
![Page 56: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/56.jpg)
Morphia
MEAN Stack
Java
Python
Perl
Ruby
Support for the most popular languages and frameworks
![Page 57: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/57.jpg)
![Page 58: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/58.jpg)
Great, I’m excited! What’s next?
![Page 59: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/59.jpg)
docs.mongodb.org
![Page 60: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/60.jpg)
Never Stop Learning!
![Page 61: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/61.jpg)
Schema Design, Schema Design, Schema Design, Schema Design!
![Page 62: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/62.jpg)
Legacy Migration
1. Copy existing schema & some data to MongoDB
2. Iterate schema design developmentMeasure performance, find bottlenecks, and embed
1. one to one associations first2. one to many associations next3. many to many associations
3. Migrate full dataset to new schema
New Software Application? Embed by default
![Page 63: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/63.jpg)
Embedding over Referencing • Embedding is a bit like pre-joined data
– BSON (Binary JSON) document ops are easy for the server
• Embed (90/10 following rule of thumb)– When the “one” or “many” objects are viewed in
the context of their parent– For performance– For atomicity
• Reference– When you need more scaling– For easy consistency with “many to many”
associations without duplicated data
![Page 64: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/64.jpg)
It’s All About Your Application
• Programs+Databases = (Big) Data Applications
• Your schema is the impedance matcher– Design choices: normalize/denormalize,
reference/embed– Melds programming with MongoDB for best of
both– Flexible for development and change
• Programs×MongoDB = Great Big Data Applications
![Page 65: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/65.jpg)
We've introduced a lot of concepts here
![Page 66: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/66.jpg)
IoT
![Page 67: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/67.jpg)
MMS @
Scale Easily
Best Practices, Automated
Cut Management Overhead
![Page 68: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/68.jpg)
Scalability @
![Page 69: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/69.jpg)
DevOps @
Provision
Upgrade
Scale
Continuous Backup
Point-in-Time Recovery
Performance Alerts
![Page 70: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/70.jpg)
Wrapping up …
• MongoDB is a great Developers Tool
• Designed for :
• Scalability• Flexibility • Performance
• Multipurpose
• Great Ecosystem
![Page 71: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/71.jpg)
Well Done !
![Page 72: Dev Jumpstart: Build Your First App with MongoDB](https://reader036.vdocument.in/reader036/viewer/2022062504/558bc3b2d8b42ac74b8b464e/html5/thumbnails/72.jpg)
Questions?