An Introduction To Mongodb: Rácz Gábor
An Introduction To Mongodb: Rácz Gábor
Rá cz Gá bor
http://www.mongodb.org/about/production-deployments/
NoSQL
• Key-value
• Graph database
• Document-oriented
• Column family
3
Document store
RDBMS MongoDB
Database Database
Table, View Collection
Row Document (JSON, BSON)
Column Field
Index Index
Join Embedded Document
Foreign Key Reference
Partition Shard
4
Document store
RDBMS MongoDB
Database Database > db.user.findOne({age:39})
Table, View Collection {
"_id" : ObjectId("5114e0bd42…"),
Row Document (JSON, BSON)
"first" : "John",
Column Field "last" : "Doe",
Index Index "age" : 39,
Join Embedded Document"interests" : [
Foreign Key Reference "Reading",
"Mountain Biking ]
Partition Shard
"favorites": {
"color": "Blue",
"sport": "Soccer"}
} 5
CRUD
• Create
• db.collection.insert( <document> )
• db.collection.save( <document> )
• db.collection.update( <query>, <update>, { upsert: true } )
• Read
• db.collection.find( <query>, <projection> )
• db.collection.findOne( <query>, <projection> )
• Update
• db.collection.update( <query>, <update>, <options> )
• Delete
• db.collection.remove( <query>, <justOne> )
6
CRUD example
> db.user.find ()
> db.user.insert({ {
first: "John", "_id" : ObjectId("51…"),
last : "Doe", "first" : "John",
age: 39 "last" : "Doe",
}) "age" : 39
}
> db.user.update(
{"_id" : ObjectId("51…")},
{
> db.user.remove({
$set: {
age: 40,
"first": /^J/
salary: 7000} }) 7
}
)
Features
• Document-Oriented storege
• Full Index Support
• Replication & High Agile
Availability
• Auto-Sharding
• Querying
• Fast In-Place Updates Scalable
• Map/Reduce
8
Memory Mapped Files
• „A memory-mapped file is a segment of virtual memory which
has been assigned a direct byte-for-byte correlation with
some portion of a file or file-like resource.”1
• mmap()
: http://en.wikipedia.org/wiki/Memory-mapped_file
1
Replica Sets Host1:10000
• Geospatial features
10
Sharding
• Partition your data
• Scale write
throughput
• Increase capacity
• Auto-balancing
shard1 shard2
Host1:10000 Host2:10010
configdb
Host3:20000
11
Host4:30000 Client
Mixed
shard1
...
Host1:10000
shardn
Host2:10001 Host4:10010
Host3:10002
replica1
configdb
Host5:20000
Host6:30000 Client
Host7:30000 12
Map/Reduce
db.collection.mapReduce(
<mapfunction>,
<reducefunction>,
{
out: <collection>,
query: <>,
sort: <>,
limit: <number>,
finalize: <function>,
scope: <>,
jsMode: <boolean>,
verbose: <boolean>
}
)
var mapFunction1 = function() { emit(this.cust_id, this.price); }; 13
var reduceFunction1 = function(keyCustId, valuesPrices)
{ return sum(valuesPrices); };
Other features
• Easy to install and use
• Detailed documentation
• Various APIs
• JavaScript, Python, Ruby, Perl, Java, Java, Scala, C#, C++, Haskell,
Erlang
• Community
• Open source
14
Theory of noSQL: CAP
• Many nodes
C
• Nodes contain replicas of
partitions of data
• Consistency
• all replicas contain the same
version of data
• Availability A P
• system remains operational on
failing nodes
• Partition tolarence CAP Theorem:
• multiple entry points satisfying all three at the
• system remains operational on same time is impossible 15
system split
Theory of noSQL: CAP
• Many nodes
C
• Nodes contain replicas of
partitions of data
• Consistency
• all replicas contain the same
version of data
• Availability A P
• system remains operational on
failing nodes
• Partition tolarence CAP Theorem:
• multiple entry points satisfying all three at the
• system remains operational on same time is impossible 16
system split
ACID - BASE
•Basically
•Atomicity
Available (CP)
•Consistency •Soft-state
•Isolation •Eventually
•Durability
consistent (AP)
17
18