Download - Building your first app with mongo db
![Page 1: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/1.jpg)
Technical Services Engineer, MongoDB
Thomas Rückstieß
#MongoDBDays
Building your first app; an introduction to MongoDB
![Page 2: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/2.jpg)
Notes to the Presenter
• Themes for this presentation:
• First presentation in the conference. Keep it fun
• User is likely brand new to MongoDB and comes from traditional relational background
• This is an introduction. Don't go deep.. Introduce concepts and refer to later presentations that build on these concepts.
![Page 3: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/3.jpg)
What is MongoDB?
![Page 4: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/4.jpg)
MongoDB is a ___________ database
• Document
• Open source
• High performance
• Horizontally scalable
• Full featured
![Page 5: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/5.jpg)
Document Database
• Not for .PDF & .DOC files
• A document is essentially an associative array with key/value pairs (possibly nested)
{ username : “thomas.r", num_logins : 39, last_login : ISODate("2013-10-08T16:46:21Z"), permissions : ["read", "write", "list", "admin"], company : { name : "MongoDB, Inc." location : "Sydney, Australia" }
}
![Page 6: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/6.jpg)
Open Source
• MongoDB is an open source project
• On GitHub
• Licensed under the AGPL
• Started & sponsored by 10gen (now MongoDB, Inc.)
• Commercial licenses available
• Contributions welcome
![Page 7: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/7.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 8: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/8.jpg)
www.etiennemansard.com
Horizontally Scalable
Shard NShard 3Shard 2Shard 1
Horizontally Scalable
![Page 9: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/9.jpg)
Full Featured
• Ad Hoc queries
• Real time aggregation
• Rich query capabilities
• Strongly consistent
• Geospatial features
• Support for most programming languages
• Flexible schema
![Page 10: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/10.jpg)
Database Landscape
Depth of Functionality
Scal
abili
ty &
Per
form
ance
Memcached
MongoDB
RDBMS
Sca
labi
lity
& P
erfo
rman
ce
![Page 11: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/11.jpg)
mongodb.org/downloads
![Page 12: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/12.jpg)
$ tar –z xvf mongodb-osx-x86_64-2.4.x.tgz
$ cd mongodb-osx-i386-2.4.4/bin
$ mkdir –p /data/db
$ ./mongod
Running MongoDB
![Page 13: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/13.jpg)
$ mongo MongoDB shell version: 2.4.4 connecting to: test > db.test.insert( { text: 'Welcome to MongoDB’ } ) > db.test.find().pretty() {
"_id" : ObjectId("51c34130fbd5d7261b4cdb55"), "text" : "Welcome to MongoDB"
}
Mongo Shell
![Page 14: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/14.jpg)
Document Database
![Page 15: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/15.jpg)
Terminology
RDBMS MongoDB
Table, View ➜ Collection
Row ➜ Document
Index ➜ Index
Join ➜ Embedded Document
Foreign Key ➜ Reference
Partition ➜ Shard
![Page 16: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/16.jpg)
Let’s Build a Blog
![Page 17: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/17.jpg)
First step in any application is
Determine your entities
![Page 18: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/18.jpg)
Entities in our Blogging System
• Users
• Articles
• Comments
• Tags
• Categories ( ? )
![Page 19: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/19.jpg)
In a relational database app We would start by doing schema design
![Page 20: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/20.jpg)
Typical (relational) ERD
User·Name·Email address
Category·Name·URL
Comment·Comment·Date·Author
Article·Name·Slug·Publish date·Text
Tag·Name·URL
![Page 21: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/21.jpg)
Relational schema design
• Large ERD Diagrams
• Complex “create table” statements
• ORMs to map tables to objects
• Tables just to join tables together
• Lots of revisions until we get it right
![Page 22: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/22.jpg)
In a MongoDB based app We start building our app and let the schema evolve
![Page 23: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/23.jpg)
MongoDB ERD
User·Name·Email address
Article·Name·Slug·Publish date·Text·Author
Comment[]·Comment·Date·Author
Tag[]·Value
Category[]·Value
![Page 24: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/24.jpg)
Working With MongoDB
![Page 25: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/25.jpg)
The Shell
![Page 26: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/26.jpg)
> var user = {
username: ’thomas.r',
first_name: ’Thomas',
last_name: ’Rückstieß',
}
Start with an object (or array, hash, dict, etc)
![Page 27: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/27.jpg)
> db test > use blog
switched to db blog // syntax is “db.<collection>.<command>”
> db.users.insert( user )
Insert the Record
No db/collection creation necessary
![Page 28: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/28.jpg)
// get one document (don’t care which one)
> db.users.findOne()
{
"_id" : ObjectId("50804d0bd94ccab2da652599"),
"username" : ”thomas.r",
"first_name" : ”Thomas",
"last_name" : ”Rückstieß"
}
Retrieve the Record again
![Page 29: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/29.jpg)
_id
• _id is the primary key in MongoDB
• Any unique immutable value could be used
• Automatically created as an ObjectId if not provided
• Automatically indexed
![Page 30: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/30.jpg)
ObjectId
• ObjectId is a special 12 byte value
• Guaranteed to be unique across your cluster
• ObjectId("50804d0bd94ccab2da652599") ts mac pid inc
![Page 31: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/31.jpg)
> db.articles.insert( {
title: ‘Hello World’,
body: ‘This is my first blog post’,
date: new Date(‘2013-06-20’),
username: ‘thomas.r’,
tags: [‘adventure’, ‘mongodb’],
comments: [ ]
} )
Creating a Blog Article
![Page 32: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/32.jpg)
> db.articles.find().pretty() {
"_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"), "title" : "Hello World", "body" : "This is my first blog post", "date" : ISODate("2013-06-20T00:00:00Z"), "username" : ”thomas.r", "tags" : [ "adventure", "mongodb" ], "comments" : [ ]
}
Finding the Article
![Page 33: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/33.jpg)
> db.articles.find( { _id : ObjectId("51c3bafafbd5d7261b4cdb5a") } )
.pretty()
{ "_id" : ObjectId("51c3bafafbd5d7261b4cdb5a"), "title" : "Hello World", "body" : "This is my first blog post", "date" : ISODate("2013-06-20T00:00:00Z"), "username" : ”thomas.r", "tags" : [ "adventure", "mongodb" ], "comments" : [ ]
}
Finding the Article
![Page 34: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/34.jpg)
> db.articles.find( { tags : 'adventure’ } ).pretty() {
"_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"), "title" : "Hello World", "body" : "This is my first blog post", "date" : ISODate("2013-06-20T00:00:00Z"), "username" : ”thomas.r", "tags" : [ "adventure", "mongodb" ], "comments" : [ ]
}
Querying An Array
![Page 35: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/35.jpg)
// the syntax is: “ update ( what, how ) “ > db.articles.update(
{ _id: ObjectId("51c3bcddfbd5d7261b4cdb5b”) }, { "$push" : { "comments" : { "name" : "Steve Noname", "comment" : "Awesome Post" } }
)
Using Update to Add a Comment
![Page 36: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/36.jpg)
> db.articles.find( { username: “thomas.r” } ).pretty() {
"_id" : ObjectId("51c3bcddfbd5d7261b4cdb5b"), "body" : "This is my first blog post", "comments" : [ { "name" : "Steve Noname", "comment" : "Awesome Post" } ], "date" : ISODate("2013-06-20T00:00:00Z"), "tags" : [ "adventure", "mongodb" ], "title" : "Hello World", "username" : ”thomas.r"
}
Article with Comment Embedded
![Page 37: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/37.jpg)
// remove comment with $pull > var last_comment = {
"name" : "Steve Noname", "comment" : "Awesome Post”
} > db.articles.update(
{ _id: ObjectId("51c3bcddfbd5d7261b4cdb5b") }, { $pull : { comments: last_comment } }
) // remove article > db.articles.remove(
{ _id: ObjectId("51c3bcddfbd5d7261b4cdb5b") } )
Remove Comments / Articles
![Page 38: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/38.jpg)
MongoDB Drivers
![Page 39: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/39.jpg)
Real applications are not built in the shell
![Page 40: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/40.jpg)
MongoDB has native bindings for over 12 languages
![Page 41: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/41.jpg)
![Page 42: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/42.jpg)
![Page 43: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/43.jpg)
![Page 44: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/44.jpg)
MongoDB Drivers
• Official Support for 12 languages
• Community drivers for tons more
• Drivers connect 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)
![Page 45: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/45.jpg)
Learn more about MongoDB
![Page 46: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/46.jpg)
Manual: docs.mongodb.org
![Page 47: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/47.jpg)
Online Training at MongoDB University
![Page 48: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/48.jpg)
We've introduced a lot of concepts here
![Page 49: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/49.jpg)
Schema Design @ 11:00
User·Name·Email address
Article·Name·Slug·Publish date·Text·Author
Comment[]·Comment·Date·Author
Tag[]·Value
Category[]·Value
![Page 50: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/50.jpg)
Replication @ 11:50
Secondary Secondary
Primary
Client ApplicationDriver
Write
Read Read
![Page 51: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/51.jpg)
Sharding @ 14:00 www.etiennemansard.com
![Page 52: Building your first app with mongo db](https://reader033.vdocuments.us/reader033/viewer/2022051013/5492290cac795920288b46ca/html5/thumbnails/52.jpg)
Indexing @ 15:50
7 16
1 2 5 6 9 12 18 21