Mongodb
High-performance, document-oriented database
documents
{
"_id" : ObjectId("51267154e4b05bd0461ed426"),
"timestamp" : NumberLong("1361473875951"),
"payload" : [
{
"oid" : "1.3.6.1.2.1.1.3.0",
"value" : "0:00:11.00",
"snmpType" : "TimeTicks"
},
{
"oid" : "1.3.6.1.6.3.1.1.4.1.0",
"value" : "1.3.6.1.6.3.1.1.5.1",
"snmpType" : "OID"
}
]
}
Collections
Are indexable aggregates of Documents
A database
Groups Collections together
(usually used by the same application)
Collections
Are like SQL tables
But they have no fixed schema!
They can contain documents with different shapes
API
JavaScript command line
> db.getCollectionNames()
[ "system.indexes", "traps" ]
> db.traps.findOne()
{"_id" : ObjectId("51267154e4b05bd0461ed426"),
"timestamp" : NumberLong("1361473875951"),
"payload" : [
{
"oid" : "1.3.6.1.2.1.1.3.0",
"value" : "0:00:11.00",
"snmpType" : "TimeTicks"
},{
"oid" : "1.3.6.1.6.3.1.1.4.1.0",
"value" : "1.3.6.1.6.3.1.1.5.1",
"snmpType" : "OID"
}]}
>
API
Query by similarity
> db.messages.find({from:"mario"})
{
"_id" : ObjectId("5187b06ba504d801ed2c70f6"),
"from" : "mario",
"to" : "luigi",
"text" : "hello mario"
}
>
api
Updates: atomic, create if not exists, create or update.
> db.getCollectionNames()
[ "system.indexes" ]
> db.messages.save({ from: "mario", to:"luigi", text:"hello mario"})
> db.getCollectionNames()
[ "messages", "system.indexes" ]
>
Replication
Replica-Set: one Primary, many Secondary
REPLICATION
Drivers makes connection to the Primary transparent.
When the Primary node goes offline, the remaining Secondaries will automatically start an Election for a new Primary.
replication
Easy setup
>rs.initiate()
>rs.add("hostname2:27017")
>rs.add("hostname3:27017", {arbiterOnly:true})
distributed querying
An example:
> var mapFunction = function() {
emit(this.from, 1);
}
> var reduceFunction = function(keySender, occurrences) {
return Array.sum(occurrences);
}
> db.messages.mapReduce(
mapFunction,
reduceFunction,
{ out: "map_reduce_example" }
)