Building Your First App: An
Introduction to MongoDB
Robert Stam
Software Engineer
MongoDB, Inc.
Great Wide Open Conference...
What is MongoDB?
3
• Document
• Open source
• High performance
• Horizontally scalable
• Full featured
MongoDB is a __________ database
4
• Not for .PDF & .DOC files
• A document is essentially an associative array
• Document == JSON object
• Document == PHP...
5
• MongoDB is an open source project
• On GitHub
• Licensed under the AGPL
• Started & sponsored by MongoDB, Inc
• Commer...
Database Landscape
7
• Written in C++
• Runs nearly everywhere
• Extensive use of memory-mapped files
i.e. read-through write-through memory ...
8
• Ad Hoc queries
• Rich query capabilities
• Geospatial features
• Real time aggregation
• Eventually consistent
• Suppo...
9
Indexing @
10
Replication @
11
Sharding @
mongodb.org/downloads
Document Database
RDBMS MongoDB
Table, View  Collection
Row  Document
Index  Index
Join  Embedded Document
Foreign Key  Reference
Parti...
Typical (relational) ERD
MongoDB ERD
We will build a library management
application
http://www.flickr.com/photos/somegeekintn/3484353131/
First step in any application
Determine your entities
19
• Library Patrons (users)
• Books (card catalog)
• Authors
• Publishers
Library Management Application
Entities
In a relational based app
We would start by doing schema
design
21
• Users
• Books
• Authors
• Publishers
MongoDB Collections
Working with MongoDB
No common programming language
so we are using the MongoDB shell
24
> var user = {
username : "fred.jones",
first_name : "fred",
last_name : "jones"
}
Start with an Object
(or array, hash...
25
> use library
> db.users.insert(user)
Insert the Record
No database or collection creation needed
26
> db.users.findOne()
{
_id : ObjectId("50804d0bd94ccab2da652599"),
username : "fred.jones",
first_name : "fred",
last_n...
27
• _id is the primary key in MongoDB
• Automatically created as an ObjectId if not
provided
• Any unique immutable value...
28
• ObjectId is a special 12 byte value
• Guaranteed to be unique across your cluster
• ObjectId("50804d0bd94ccab2da65259...
29
> db.author.insert({
first_name : "j.r.r.",
last_name : "tolkien",
bio : "J.R.R. Tolkien (1892.1973), beloved
throughou...
30
> db.books.insert({
title : "fellowship of the ring, the",
author : ObjectID("507ffbb1d94ccab2da652597"),
language : "e...
31
> db.books.findOne(
{ language : "english" },
{ genre : 1 })
{
"_id" : ObjectID("50804391d94ccab2da652598"),
"genre" : ...
32
> db.books.findOne(
{ genre : "fantasy" },
{ title : 1 })
{
"_id" : ObjectID("50804391d94ccab2da652598"),
"title" : "fe...
33
> db.books.findOne({}, { publication : 1 })
{
"_id" : ObjectID("50804391d94ccab2da652598"),
"publication" : {
"name" : ...
34
> db.books.update(
{ _id : ObjectID("50804391d94ccab2da652598") },
{ $set : {
isbn : "0547928211",
pages : 432
}}
)
Tru...
35
> db.books.findOne()
{
"_id" : ObjectId("50804391d94ccab2da652598"),
"author" : ObjectId("507ffbb1d94ccab2da652597"),
"...
36
> db.books.ensureIndex({ title : 1 })
> db.books.ensureIndex({ genre : 1 }) // multikey
> db.books.ensureIndex({ "publi...
37
> db.books.findOne({ title : /^fell/ })
{
"_id" : ObjectId("50804391d94ccab2da652598"),
"author" : ObjectId("507ffbb1d9...
38
> db.books.insert({
title : "two towers, the",
author : ObjectID("507ffbb1d94ccab2da652597"),
language : "english",
isb...
39
> db.books.find(
{ author : ObjectId("507ffbb1d94ccab2da652597") })
.sort({ "publication.date" : -1 })
.limit(1)
{
"_id...
40
> var page_num = 3;
> var results_per_page = 10;
> var cursor = db.books.find()
.sort({ publication_date : -1 })
.skip(...
41
> var book = db.books.findOne(
{ title : "return of the king, the" })
> db.author.findOne({ _id : book.author })
{
"_id...
MongoDB Drivers
Real Applications are not built in
the shell
MongoDB has native bindings
for over 12 languages
47
• Official Support for 12 languages
• Community drivers for many more
• Drivers connect applications to mongo servers
•...
48
docs.mongodb.org
49
Online Training at MongoDB University
Questions?
MongoDB World
New York City, June 23-25
Save 25% with Drivers25
Register at world.mongodb.com
See how Cisco, Stripe, Carfa...
MongoDB World
New York City, June 23-25
See what’s next in MongoDB including
• MongoDB 2.6
• Sharding
• Replication
• Aggr...
Software Engineer, MongoDB Inc.
Robert Stam
Thank You
Building Your First App: An Introduction to MongoDB
Building Your First App: An Introduction to MongoDB
Upcoming SlideShare
Loading in …5
×

Building Your First App: An Introduction to MongoDB

440
-1

Published on

Great Wide Open - Day 1
Robert Stam - MongoDB
2:30 PM - Operations 1 (Databases)

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
440
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Building Your First App: An Introduction to MongoDB

  1. 1. Building Your First App: An Introduction to MongoDB Robert Stam Software Engineer MongoDB, Inc. Great Wide Open Conference Atlanta, April 2, 2014
  2. 2. What is MongoDB?
  3. 3. 3 • Document • Open source • High performance • Horizontally scalable • Full featured MongoDB is a __________ database
  4. 4. 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
  5. 5. 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
  6. 6. Database Landscape
  7. 7. 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
  8. 8. 8 • Ad Hoc queries • Rich query capabilities • Geospatial features • Real time aggregation • Eventually consistent • Support for many programming languages • Flexible schema Full Featured
  9. 9. 9 Indexing @
  10. 10. 10 Replication @
  11. 11. 11 Sharding @
  12. 12. mongodb.org/downloads
  13. 13. Document Database
  14. 14. RDBMS MongoDB Table, View  Collection Row  Document Index  Index Join  Embedded Document Foreign Key  Reference Partition  Shard Terminology
  15. 15. Typical (relational) ERD
  16. 16. MongoDB ERD
  17. 17. We will build a library management application http://www.flickr.com/photos/somegeekintn/3484353131/
  18. 18. First step in any application Determine your entities
  19. 19. 19 • Library Patrons (users) • Books (card catalog) • Authors • Publishers Library Management Application Entities
  20. 20. In a relational based app We would start by doing schema design
  21. 21. 21 • Users • Books • Authors • Publishers MongoDB Collections
  22. 22. Working with MongoDB
  23. 23. No common programming language so we are using the MongoDB shell
  24. 24. 24 > var user = { username : "fred.jones", first_name : "fred", last_name : "jones" } Start with an Object (or array, hash, dict, etc)
  25. 25. 25 > use library > db.users.insert(user) Insert the Record No database or collection creation needed
  26. 26. 26 > db.users.findOne() { _id : ObjectId("50804d0bd94ccab2da652599"), username : "fred.jones", first_name : "fred", last_name : "jones" } Querying for the user
  27. 27. 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
  28. 28. 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
  29. 29. 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
  30. 30. 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
  31. 31. 31 > db.books.findOne( { language : "english" }, { genre : 1 }) { "_id" : ObjectID("50804391d94ccab2da652598"), "genre" : [ "fantasy", "adventure" ] } Multiple Values per Key
  32. 32. 32 > db.books.findOne( { genre : "fantasy" }, { title : 1 }) { "_id" : ObjectID("50804391d94ccab2da652598"), "title" : "fellowship of the ring, the" } Querying for key with multiple values
  33. 33. 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
  34. 34. 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
  35. 35. 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
  36. 36. 36 > db.books.ensureIndex({ title : 1 }) > db.books.ensureIndex({ genre : 1 }) // multikey > db.books.ensureIndex({ "publication.date" : 1 }) Creating Indexes
  37. 37. 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
  38. 38. 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
  39. 39. 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
  40. 40. 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
  41. 41. 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
  42. 42. MongoDB Drivers
  43. 43. Real Applications are not built in the shell
  44. 44. MongoDB has native bindings for over 12 languages
  45. 45. 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
  46. 46. 48 docs.mongodb.org
  47. 47. 49 Online Training at MongoDB University
  48. 48. Questions?
  49. 49. MongoDB World New 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
  50. 50. MongoDB World New 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
  51. 51. Software Engineer, MongoDB Inc. Robert Stam Thank You
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×