In the blog NoSQL, I provided an introduction to NoSql databases. We have discussed some NoSql databases such as HBase, Cassandra , Redis. In this blog, we discuss MongoDB, a document oriented database, which is in contrast to the key value stores we discussed earlier. MongoDB is currently one of the more popular NoSql databases, primarily due to its ease of use and simpler programming model. But there have been reports that it lags in scalability or performance compared to other NoSql databases. And it has more moving parts. But its ease of use and low learning curve makes it an attractive choice in many scenarios.
The key features of MongoDB are:
Step 1: Download Mongo
You can download the server from www.mongodb.org/downloads.
I like to download the generic linux version and untar it.
Untar/unzip it to a directory of your choice.
Step 2 : Start the server
Decide on a directory to store the data. Say ~/mongodata. Create the directory.
Change to the directory where you installed mongo. To start the server, type the command.
bin/mongod -dbpath ~/mongodata
Step 3: Start the mongo client
bin/mongo
Step 4: Create and insert some data into a collection
Create and use a database.
> use testDb ;
Create a employee document and insert into the employees collection.
> emp1 = { "employee_id":"12345", "name":"John doe", "department": "database team", "title":"architect", "start_date":"1/1/2015" }
> db.employees.insert(emp1)
Retrieve the document.
> db.employees.find()
{ "_id" : ObjectId("54c2de34426d3d4ea1226498"), "employee_id" : "12345", "name" : "John doe", "department" : "database team", "title" : "architect", "start_date" : "1/1/2015" }
Step 5 : Insert a few more employees
> emp2 = { "employee_id":"12346", "name":"Ste Curr", "department": "database team", "title":"developer1", "start_date":"12/1/2013" }
> db.employees.insert(emp2)
> emp3 = { "employee_id":"12347", "name":"Dre Grin", "department": "QA team", "title":"developer2", "start_date":"12/1/2011" }
> db.employees.insert(emp3)
> emp4 = { "employee_id":"12348", "name":"Daev Eel", "department": "Build team", "title":"developer3", "start_date":"12/1/2010" }
> db.employees.insert(emp4)
Step 6: Queries
Query by attribute equality
> db.employees.find({"name" : "Ste Curr"} )
{ "_id" : ObjectId("54c2e0de426d3d4ea1226499"), "employee_id" : "12346", "name" : "Ste Curr", "department" : "database team", "title" : "developer1", "start_date" : "12/1/2013" }
Query by attribute with regex condition
> db.employees.find({"department":{$regex : "data*"}})
{ "_id" : ObjectId("54c2de34426d3d4ea1226498"), "employee_id" : "12345", "name" : "John doe", "department" : "database team", "title" : "architect", "start_date" : "1/1/2015" }
{ "_id" : ObjectId("54c2e0de426d3d4ea1226499"), "employee_id" : "12346", "name" : "Ste Curr", "department" : "database team", "title" : "developer1", "start_date" : "12/1/2013" }
Query using less than , greater than conditions
> db.employees.find({"employee_id":{$gte : "12347"}})
{ "_id" : ObjectId("54c2e382426d3d4ea122649a"), "employee_id" : "12347", "name" : "Dre Grin", "department" : "QA team", "title" : "developer2", "start_date" : "12/1/2011" }
{ "_id" : ObjectId("54c2e3af426d3d4ea122649b"), "employee_id" : "12348", "name" : "Daev Eel", "department" : "Build team", "title" : "developer3", "start_date" : "12/1/2010" }
> db.employees.find({"employee_id":{$lte : "12346"}})
{ "_id" : ObjectId("54c2de34426d3d4ea1226498"), "employee_id" : "12345", "name" : "John doe", "department" : "database team", "title" : "architect", "start_date" : "1/1/2015" }
{ "_id" : ObjectId("54c2e0de426d3d4ea1226499"), "employee_id" : "12346", "name" : "Ste Curr", "department" : "database team", "title" : "developer1", "start_date" : "12/1/2013" }
Step 7: Cursors
Iterate through results.
> var techguys = db.employees.find()
> while ( techguys.hasNext() ) printjson( techguys.next() )
{
"_id" : ObjectId("54c2de34426d3d4ea1226498"),
"employee_id" : "12345",
"name" : "John doe",
"department" : "database team",
"title" : "architect",
"start_date" : "1/1/2015"
}
.
.
.
Step 8: Delete records
Delete one record
> db.employees.remove({"employee_id" : "12345"})
WriteResult({ "nRemoved" : 1 })
Delete all records
> db.employees.remove({})
WriteResult({ "nRemoved" : 3 })
As you can see MongoDb is pretty easy to use. Download and give it a try.
The key features of MongoDB are:
- The unit of storage like a record in relational databases or key-value pair in key value stores, is a document or more precisely a JSON document.
- { "employee_id":"12345",
- "name":"John doe",
- "department": "database team",
- "title":"architect",
- "start_date":"1/1/2015" }
- Documents are stored in collections.
- Collection can be indexed by field.
- Indexing support for faster queries.
- No schema is required for the collection.
- MongoDB is highly available using replication and automatic failover. Write happens to a primary server but can be replicated to multiple replicas. If the primary goes down, one of the replicas takes over as the primary.
- Read operations can be scaled by sending the reads to the replicas as well.
- Write operations are scaled by sharding.
- Sharding is automatic.But has a couple of moving parts
- Sharding is based on a key which is an indexed field or a indexed compound field.
- Sharding can be range based or hash based. With range based, partitioning is based on key range, so that values close to each other are together. With Hash based, the partioning is based on a hash of the key.
- Data set is divided into chunks. Each shard manages some chunks
- Query routers are used to send the request to the right shard.
- Config servers hold meta data on which chunks are with which shard.
- If a chunk grows too large, it is broken up. If some shards own more chunks than others, the cluster is automatically rebalanced by redistributing the chunks.
Step 1: Download Mongo
You can download the server from www.mongodb.org/downloads.
I like to download the generic linux version and untar it.
Untar/unzip it to a directory of your choice.
Step 2 : Start the server
Decide on a directory to store the data. Say ~/mongodata. Create the directory.
Change to the directory where you installed mongo. To start the server, type the command.
bin/mongod -dbpath ~/mongodata
Step 3: Start the mongo client
bin/mongo
Step 4: Create and insert some data into a collection
Create and use a database.
> use testDb ;
Create a employee document and insert into the employees collection.
> emp1 = { "employee_id":"12345", "name":"John doe", "department": "database team", "title":"architect", "start_date":"1/1/2015" }
> db.employees.insert(emp1)
Retrieve the document.
> db.employees.find()
{ "_id" : ObjectId("54c2de34426d3d4ea1226498"), "employee_id" : "12345", "name" : "John doe", "department" : "database team", "title" : "architect", "start_date" : "1/1/2015" }
Step 5 : Insert a few more employees
> emp2 = { "employee_id":"12346", "name":"Ste Curr", "department": "database team", "title":"developer1", "start_date":"12/1/2013" }
> db.employees.insert(emp2)
> emp3 = { "employee_id":"12347", "name":"Dre Grin", "department": "QA team", "title":"developer2", "start_date":"12/1/2011" }
> db.employees.insert(emp3)
> emp4 = { "employee_id":"12348", "name":"Daev Eel", "department": "Build team", "title":"developer3", "start_date":"12/1/2010" }
> db.employees.insert(emp4)
Step 6: Queries
Query by attribute equality
> db.employees.find({"name" : "Ste Curr"} )
{ "_id" : ObjectId("54c2e0de426d3d4ea1226499"), "employee_id" : "12346", "name" : "Ste Curr", "department" : "database team", "title" : "developer1", "start_date" : "12/1/2013" }
Query by attribute with regex condition
> db.employees.find({"department":{$regex : "data*"}})
{ "_id" : ObjectId("54c2de34426d3d4ea1226498"), "employee_id" : "12345", "name" : "John doe", "department" : "database team", "title" : "architect", "start_date" : "1/1/2015" }
{ "_id" : ObjectId("54c2e0de426d3d4ea1226499"), "employee_id" : "12346", "name" : "Ste Curr", "department" : "database team", "title" : "developer1", "start_date" : "12/1/2013" }
Query using less than , greater than conditions
> db.employees.find({"employee_id":{$gte : "12347"}})
{ "_id" : ObjectId("54c2e382426d3d4ea122649a"), "employee_id" : "12347", "name" : "Dre Grin", "department" : "QA team", "title" : "developer2", "start_date" : "12/1/2011" }
{ "_id" : ObjectId("54c2e3af426d3d4ea122649b"), "employee_id" : "12348", "name" : "Daev Eel", "department" : "Build team", "title" : "developer3", "start_date" : "12/1/2010" }
> db.employees.find({"employee_id":{$lte : "12346"}})
{ "_id" : ObjectId("54c2de34426d3d4ea1226498"), "employee_id" : "12345", "name" : "John doe", "department" : "database team", "title" : "architect", "start_date" : "1/1/2015" }
{ "_id" : ObjectId("54c2e0de426d3d4ea1226499"), "employee_id" : "12346", "name" : "Ste Curr", "department" : "database team", "title" : "developer1", "start_date" : "12/1/2013" }
Step 7: Cursors
Iterate through results.
> var techguys = db.employees.find()
> while ( techguys.hasNext() ) printjson( techguys.next() )
{
"_id" : ObjectId("54c2de34426d3d4ea1226498"),
"employee_id" : "12345",
"name" : "John doe",
"department" : "database team",
"title" : "architect",
"start_date" : "1/1/2015"
}
.
.
.
Step 8: Delete records
Delete one record
> db.employees.remove({"employee_id" : "12345"})
WriteResult({ "nRemoved" : 1 })
Delete all records
> db.employees.remove({})
WriteResult({ "nRemoved" : 3 })
As you can see MongoDb is pretty easy to use. Download and give it a try.