3. MongoDB is a ___________
database
• Document
• Open source
• High performance
• Horizontally scalable
• Full featured
4. 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
5. Open Source
• MongoDB is an open source project
• On GitHub
• Licensed under the AGPL
• Started & sponsored by 10gen
• Commercial licenses available
• Contributions welcome
6. 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
8. Full Featured
• Ad Hoc queries
• Real time aggregation
• Rich query capabilities
• Traditionally consistent
• Geospatial features
• Support for most programming languages
• Flexible schema
19. In a relational based app
We would start by doing
schema design
20. 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
21. In a MongoDB based app
We start building our
and let the schema evolve
app
27. Querying for the User
> db.users.findOne()
{
"_id" : ObjectId("50804d0bd94ccab2da652599"),
"username" : "fred.jones",
"first_name" : "fred",
"last_name" : "jones"
}
28. _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
29. ObjectId
• ObjectId is a special 12 byte value
• Guaranteed to be unique across your cluster
• ObjectId("50804d0bd94ccab2da652599")
|-------------||---------||-----||----------|
ts mac pid inc
30. Creating an Author
> 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.'
})
31. Querying for Our Author
> 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."
}
32. Creating a Book
> 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'),
}
})
http://society6.com/PastaSoup/The-Fellowship-of-the-Ring-ZZc_Print/
34. Multiple Values Per Key
> db.books.findOne({genre: 'fantasy'}, {title: 1})
{
"_id" : ObjectId("50804391d94ccab2da652598"),
"title" : "fellowship of the ring, the"
}
Query key with single value or
multiple values the same way.
36. Query Nested Values with
Dot Notation
> 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")
}
}
37. Update Books
> db.books.update(
{"_id" : ObjectId("50804391d94ccab2da652598")},
{
$set : {
isbn: '0547928211',
pages: 432
}
})
This is true agile development .
I’m simply changing how I work
with the data, and the
database follows.
45. Finding an Author by Book
> 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."
}
48. MongoDB Drivers
• Drivers connect to mongo servers
• Drivers translate BSON into native types
• The MongoDB shell is not a driver, but works like
one in some ways
• Installed using typical means (npm, pecl, gem,
pip)
Introduction and QuestionsHow many are currently using MongoDB?How many of you came from a relational database background?Emphasize the messagePreview of our talkLook at what makes MongoDB differentWalk through building your first application with MongoDB
Let’s talk about what we mean by this….
What do we mean by document?
AGPL – GNU Affero General Public License
* Big endian and ARM not supported.
Where can you get it?
Easy to use and interact withNo additional tools required, just jump right into the shell
Entity-relationship diagram
Kristine to update this graphic at some point
We’re going to get nerdy hereThe books on this shelf should be a strong indication of what’s to comeIf you happen to be one of his arch enemies, apologies for the appearance of Wil Wheaton’s name
Whats the first step?
Ask question: Is categories it's own entity? It could be, but it's likely a property of books.
So this is where the fun would end in a traditional relational database. Schema design slows you down. With mongodb, you do still need to think about how you want to store and access your data effectively, but there’s no up front commitment on the developers part. We can jump right into the building it.
Ask question: Is categories it's own entity? It could be, but it's likely a property of books. What are some pain points of relational db schema design?
This doesn’t mean we don’t think about schema, it just means that we let it evolve with out application. In MongoDB schemas are flexible and based on a different idea. Schemas are about how you use the data, not about how you store the data.So what kind of collections would be build in MongoDB…
Point out entities and what we end up doing them. We don’t have to normalize them.Do we want to query by publishers? Or do we just want the publisher information along without books?
I’m sure we have developers with many different backgrounds in the room today. Finding a common language would be challenging, so we’re going to use the Mongo Shell directly to build this app.
What is that _id field? I didn’t add that?
"50804d0bd94ccab2da652599" is a 24 byte string (12 byte ObjectId hex encoded).See how easy MongoDB makes this for you? Agile development etcLets get back to our app.
Powerful message here. Finally a database that enables rapid & agile development.
Creating a book here. A few things to make note of here…Author referenceGenre (List)Publication (Embedded Document)Date (new type, why not ISODate?)How would I query on genre? How do I work with keys/document fields that might have multiple values?
Specify which fields are returned. Default is everything
It’s all the same!
Feels familiar, very intuitive.How about updating the book? What if I need to add something new to this object?
Powerful message here. Finally a database that enables rapid & agile development.
You can see the updated fields here.What about indexes? Right now, we only have an index on _id because MongoDB does that for us automatically.If I don’t have to create my collection ahead of time and specify a strict schema, how do I define indexes and optimize my queries?
Creating a book here. A few things to make note of.
Creating a book here. A few things to make note of.
This is fine for small result sets. Not good performance for large result sets. Range query plus limit will give better performance.
The shell is cool and all, but how do I use MongoDB in language X?
Raise your hand if your language of choice is on this screen?