Type: Document Database
Designed as a scalable database (humongous)
Open Source
Released: 2009
Database
Collection
RDBMS
Schema
Instance
MongoDB
Instance
Table
Document
Row
> printjson(db.towns.findOne({"_id":ObjectId("4d0b6da3bb30773266f39fea")}))
{
"_id":ObjectId("4d0b6da3bb30773266f39fea"),
"country": {
"$ref": "countries",
"$id": ObjectId("4d0e6074deb899526a8309e")
},
"famous_for": [
"beer",
"food"
],
"last_census": "Thu Sep 20 2007 00:00:00 GMT -0700 (PDT)",
"mayor": {
"name": "Sam Adams",
"party": "D"
},
"name": "Portland",
"population": 582000,
"state": "OR"
}Stored as BSON - Shown as JSON
Database: "db"
Collection: "towns"
Document: Between the brackets
Identifier: "_id"
0-3: Timestamp 4-6: Client Machine ID 7-8: Client Process ID 9-11: Incremented Counter
Notations
ObjectId (12 bytes)
> db.test.insert({color: "Red"})
> db.test.insert({shape: "Triangle"})Insert
> db.test.find()
{
"_id" : ObjectId("55ee924b34ec1a858b481962"),
"color" : "Red"
}
{
"_id" : ObjectId("55ee927f34ec1a858b481963"),
"shape" : "Triangle"
}Find
> db.test.find({ "_id" : ObjectId("55ee927f34ec1a858b481963") })
{
"_id" : ObjectId("55ee927f34ec1a858b481963"),
"shape" : "Triangle"
}Find by ID
> db.test.find( { "shape": "Triangle"} )
{
"_id" : ObjectId("55ee927f34ec1a858b481963"),
"shape" : "Triangle"
}Find by field (slower..)
> db.test.find( function() {
return this.shape == "Triangle";
})
{
"_id" : ObjectId("55ee927f34ec1a858b481963"),
"shape" : "Triangle"
}Find by function (slowest!)
| Command | Description |
|---|---|
| $regex | Match by regular expression |
| $lt | Less than |
| $in | Match any elements in an array |
| $exists | Check for existence of a field |
Examples for find
| Command | Description |
|---|---|
| $set | Sets the given field with a given value |
| $push | Adds the value to an array |
| $pull | Removes matching value from array |
Examples for update
Mongo is not built for performing joins because of its distributed nature!
Documents can however reference eachother.
> db.towns.update(
{ _id : ObjectId("4d0ada87bb30773266f39fe5") },
{ $set : { country: { $ref: "countries", $id: "us" } } }
)
THEN
> var portland = db.towns.findOne({ _id : ObjectId("4d0ada87bb30773266f39fe5") })
> db.countries.findOne({ _id: portland.country.$id })
OR
>
db[ portland.country.$ref ].findOne({ _id: portland.country.$id })Query performance can be increased in MongoDB by using indexing.
Full collection scan
B-tree indexing
Scanned objects: 1 000 000
Scanned objects: 1
Execution Time 1st: 470 ms
Execution Time 2nd: 365 ms
Execution Time 1st: 5 ms
Execution Time 2nd: 0 ms
> populatePhones(800, 809, 5550000, 5650000)
> db.phones.find().limit(1)
{
"_id" : 18005550000,
"components" : {
"country" : 1,
"area" : 800,
"prefix" : 555,
"number" : 5550000 },
"display" : "+1 800-5550000"
}What makes document databases unique?
Their ability to efficiently handle arbitrary nested, schemaless data documents.
What makes MongoDB special?
The ability to scale across several servers by replicating or sharding collections.
City of Chicago
McAfee
Crittercism
Otto
Under Armour
Carfax
Forbes
Ebay
Pearson