Академический Документы
Профессиональный Документы
Культура Документы
What is MongoDB
Document Database
Not for .PDF & .DOC files
A document is essentially an associative array Document == JSON object Document == PHP Array Document == Python Dict
Open Source
MongoDB is an open source project
On GitHub Licensed under the AGPL Started & sponsored by 10gen Commercial licenses available
Contributions welcome
High Performance
Written in C++
Extensive use of memory-mapped files
Horizontally Scalable
Full Featured
Ad Hoc queries
Real time aggregation Rich query capabilities Traditionally consistent Geospatial features
Database Landscape
http://www.mongodb.org/download s
Mongo Shell
Document Database
Terminology
MongoDB ERD
tables
Lots of revisions until we get it just right
MongoDB collections
Users
Books Authors Publishers
user = {
username: 'fred.jones', first_name: 'fred',
last_name: 'jones',
}
_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
ObjectId
ObjectId is a special 12 byte value
Guaranteed to be unique across your cluster ObjectId("50804d0bd94ccab2da652599")
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.' })
"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 a Book
> db.books.insert({
title: 'fellowship of the ring, the', author: ObjectId("507ffbb1d94ccab2da652597"), language: 'english',
"fantasy",
"adventure" ] }
Query key with single value or multiple values the same way.
Nested Values
> db.books.findOne({}, {publication: 1})
{ "_id" : ObjectId("50804ec7d94ccab2da65259a"), "publication" : {
Update books
> 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
Creating indexes
> db.books.ensureIndex({title: 1})
> db.books.ensureIndex({genre : 1})
> db.books.ensureIndex({'publication.date': -1})
Cursors
> 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-20T04:00:00Z") } }
Paging
page_num = 3;results_per_page = 10;cursor = db.books.find() .sort({ "publication.date" : -1 }) .skip((page_num - 1) * results_per_page) .limit(results_per_page);
MongoDB Drivers
MongoDB drivers
Official Support for 12 languages
Community drivers for tons more Drivers connect to mongo servers Drivers translate BSON into native types Installed using typical means (npm, pecl, gem,
pip)
Next Steps
Schema Design
Indexing
Replication
Sharding
#MongoDBHelsinki - @m4rcsch
Questions?
Marc Schwering
Solutions Architect, 10gen