Building Your First App: An Introduction to MongoDB
Post on 26-Jan-2015
106 Views
Preview:
DESCRIPTION
Transcript
Building Your First App: An
Introduction to MongoDB
Robert StamSoftware Engineer
MongoDB, Inc.
Great Wide Open Conference
Atlanta, April 2, 2014
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 Array
• Document == Python Dict
• Document == Ruby Hash
• Document == C# BsonDocument object model
• etc
Document Database
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
Database Landscape
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
• Ad Hoc queries
• Rich query capabilities
• Geospatial features
• Real time aggregation
• Eventually consistent
• Support for many programming languages
• Flexible schema
Full Featured
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
Partition Shard
Terminology
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, dict, etc)
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_name : "jones"
}
Querying for the user
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
• 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
> 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
> 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
> db.books.findOne(
{ language : "english" },
{ genre : 1 })
{
"_id" : ObjectID("50804391d94ccab2da652598"),
"genre" : [
"fantasy",
"adventure"
]
}
Multiple Values per Key
32
> db.books.findOne(
{ genre : "fantasy" },
{ title : 1 })
{
"_id" : ObjectID("50804391d94ccab2da652598"),
"title" : "fellowship of the ring, the"
}
Querying for key with
multiple values
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
> 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
> 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
> db.books.ensureIndex({ title : 1 })
> db.books.ensureIndex({ genre : 1 }) // multikey
> db.books.ensureIndex({ "publication.date" : 1 })
Creating Indexes
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
> 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
> 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
> 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
> 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
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
• 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
48
docs.mongodb.org
49
Online Training at MongoDB University
Questions?
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
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
Software Engineer, MongoDB Inc.
Robert Stam
Thank You
top related