This talk will introduce the philosophy and features of the open source, NoSQL 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.
2. 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.
4. MongoDB is a ___________ database
• Document
• Open source
• High performance
• Horizontally scalable
• Full featured
5. 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"
}
}
6. 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
7. 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
9. Full Featured
• Ad Hoc queries
• Real time aggregation
• Rich query capabilities
• Strongly consistent
• Geospatial features
• Support for most programming languages
• Flexible schema
21. 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
22. In a MongoDB based app
We start building our app
and let the schema evolve
26. Start with an object
(or array, hash, dict, etc)
> var user = {
username: ’thomas.r',
first_name: ’Thomas',
last_name: ’Rückstieß',
}
27. Insert the Record
> db
test
> use blog
switched to db blog
// syntax is “db.<collection>.<command>”
> db.users.insert( user )
No db/collection creation necessary
28. Retrieve the Record again
// get one document (don’t care which one)
> db.users.findOne()
{
"_id" : ObjectId("50804d0bd94ccab2da652599"),
"username" : ”thomas.r",
"first_name" : ”Thomas",
"last_name" : ”Rückstieß"
}
29. _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
30. ObjectId
• ObjectId is a special 12 byte value
• Guaranteed to be unique across your cluster
• ObjectId("50804d0bd94ccab2da652599")
ts
mac pid
inc
31. Creating a Blog Article
> 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: [ ]
})
32. Finding the Article
> 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" : [ ]
}
33. Finding the Article
> 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" : [ ]
}
34. Querying An Array
> 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" : [ ]
}
35. Using Update to Add a Comment
// the syntax is: “ update ( what, how ) “
> db.articles.update(
{ _id: ObjectId("51c3bcddfbd5d7261b4cdb5b”) },
{ "$push" : {
"comments" : {
"name" : "Steve Noname",
"comment" : "Awesome Post"
}
}
)
44. 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)