Document Oriented Database
Document Oriented Database
Not Really
Document is similar to row or record in relation DB, but more flexible. Retrieve documents based on their contents. Different DB offers variety ways to organize documents:
Collections Tags Non-visible Metadata Directory hierarchies
MongoDB
MongoDB wasnt designed in a lab. We built MongoDB from our own experiences building large scale, high availability, robust systems. We didnt start from scratch, we really tried to figure out what was broken, and tackle that. So the way I think about MongoDB is that if you take MySql, and change the data model from relational to document based, you get a lot of great features: embedded docs for speed, manageability, agile development with schema-less databases, easier horizontal scalability because joins arent as important. There are lots of things that work great in relational databases: indexes, dynamic queries and updates to name a few, and we havent changed much there. For example, the way you design your indexes in MongoDB should be exactly the way you do it in MySql or Oracle, you just have the option of indexing an embedded field.
MongoDB
MongoDB wasnt designed in a lab. We built MongoDB from our own experiences building large scale, high availability, robust systems. We didnt start from scratch, we really tried to figure out what was broken, and tackle that. So the way I think about MongoDB is that if you take MySql, and change the data model from relational to document based, you get a lot of great features: embedded docs for speed, manageability, agile development with schema-less databases, easier horizontal scalability because joins arent as important. There are lots of things that work great in relational databases: indexes, dynamic queries and updates to name a few, and we havent changed much there. For example, the way you design your indexes in MongoDB should be exactly the way you do it in MySql or Oracle, you just have the option of indexing an embedded field.
MongoDB wasnt designed in a lab. We built MongoDB from our own experiences building large scale, high availability, robust systems . So the way I think about MongoDB is that if you take MySql, and change the data model from relational to document based, you get a lot of great features Eliot Horowitz, 10gen CTO and Co-founder
MongoDB Overview
Schema-free document database Written in C++ open-source project that mainly driven by 10gen Inc. 10gen Inc. also offers professional services around MongoDB. Has driver to all most every popular language programming.
MongoDB in many ways feels like an RDMS. Its easy to learn and quick to implement.
Semi-structured Data
MongoDB is NOT a key/value store. Store complex documents as arrays, hash tables, integers, objects and every thing else supported by JSON:
2.
5.
Easy to Query
MongoDB can apply most updates over the existing row, keeping the index and data structure relatively untouched and do so VERY FAST.
Data is distributed in 4 or 16MB chunk sand stored redundantly in your MongoDB network.
Capped collections
Fixed-size round robin tables with extremely fast reads and writes. Perfect for: Logging Messaging Job Queues Caching Features: Automatically ages out old data Canal so query, delete and update out of FIFO order FIFO reads/writes are nearly as fast as cat> file; tailf/file Tailable cursor stays open as reads rows as they are added Persistent, fault-tolerant, distributed Atomic pop items off the stack
Getting Started
You can install MondoDB in two main ways:
Run it like regular exe program. Install it like service in your OS.
Install MongoDB
1. Download MongoDB. 2. Extract it. 3. Create the data folder. 4. Run mongod.exe
Usually /data/db or- C:\data\db www.mongodb.com/downloads
DB Connction
Mongo m = new Mongo(); // or Mongo m = new Mongo( "localhost" ); // or Mongo m = new Mongo( "localhost" , 27017 ); //getting DB DB db = m.getDB( "mydb" );
Authentication
When you create a connection to the DB you can use authentication and actually use this DB as the most popular RDMS (like Oracle and MSSQL). Authentication is optional.
Collections
Each DB in Mongo DB composed of collections. Each Collection contains documents (recommended with the same structure). Once you have this collection object, you can do things like insert data, query for data, etc. //getting a collction DBCollection coll = db.getCollection("testCollection");
Primary Key
Almost every MongoDB document has an _id field as its first attribute. The _id value is the primary key in MongoDB, it can be of any type except arrays. If a user tries to insert a document without providing an _id field, the database will automatically generate an _object id_ and store it the _id field. All the MongoDB Drivers use ObjectId as the default _id value.
Insert A Document
//Creating a document BasicDBObject doc = new BasicDBObject(); doc.put("name", "MongoDB"); doc.put("type", "database"); doc.put("count", 1);
//Creating embedded document BasicDBObject info = new BasicDBObject(); info.put("x", 203); info.put("y", 102); doc.put("info", info);
Update A Document
//update single document basicDBObject query = new basicDBObject (_id, order_id); basicDBObject fildesToUpdate = new basicDBObject (state, cart); Coll.update(query, fildesToUpdate); //update multiple documents. basicDBObject query = QueryBuilder.start(subtotal).graterthen(2500); basicDBObject fildesToUpdate = new basicDBObject (state, cart); Coll.update(query, fildesToUpdate, false, true);
Remove A Document
Removing document is very simple. You just need to use the remove method
//example //remove all the objects in the collection. coll(name).remove(); //remove specific object from the collection coll(name).remove(query);
Finding A Document
The basic way to get document from collection is:
//findone() DBObject myDoc = coll.findOne(); //or find() //return all the documents in the collection DBCursor cur = coll.find(); //return the documents that much to the query cur = coll.find(query);
DBCursor
The find function return class type of DBCursor. The DBCursor is an iterator to the query result list. You can use the DBCursor as a regular java iterator and run all over the results in very simple way.
QueryBuilder Example
//Get all the capital cities with 10000000 or more citizens DBObject o; o = QueryBuilder. start("population").greaterThan(10000000).and("capital").is(true) .get(); DBCursor answer = db.getCollection("Cities").find(o);
Aggregate
Choosing MongoDB meant giving up on SQLs aggregate functions (AVG(), MAX(), MIN(), SUM(), etc.). You need to implement this function in your client side. MongoDB give you just one aggregation function called count() that retrieve the number of documents that match to some query.
Group By
BasicDBObject key = new BasicDBObject(); BasicDBObject initial = new BasicDBObject(); initial.put("totalPopulation", 0); DBObject cond1 = QueryBuilder.start("_id").notEquals("ee").get(); String reduceString = "function(obj,prev) { prev.totalPopulation += obj.population; }"; BasicDBList cur = (BasicDBList) db.getCollection("Countries") .group(key, cond1, initial, reduceString);
Order By
To do order by in mongoDB you need to use the sort() method. The method get an ObjectDB with the fields to order by and -1 to order descending or 1 to order ascending.
//order by example
Indexes
//example of creating index BasicDBObject index = new BasicDBObject("country1", -1); index.put("country2", -1); index.put("lenght", 1); db.getCollection("Borders").ensureIndex(index, "borderNieg", true);
Pros
Scalable, high performance database with familiar RDMS functionality Semi-structured (hash tables, lists, dates, ) Full, range and nested Indexes Replication and distributed storage Query language and Map/Reduce GridFS file storage (NFS replacement) BSON Serialization Capped Collections A lot of drivers to program language. Simple to install
Cons
Map/Reduce is single process (soon to be resolved) Low Availability Give up on aggregation function like SUM(), MAX(), MIN(), etc. Complex syntax for group by function
Comparison To Similar DB
CouchDB
Best used: For accumulating, occasionally changing data, on which pre-defined queries are to be run. Places where versioning is important. For example: CRM, CMS systems. Master-master replication is an especially interesting feature, allowing easy multi-site deployments.
Redis
Best used: For rapidly changing data with a foreseeable database size (should fit mostly in memory). For example: Stock prices. Analytics. Real-time data collection. Real-time communication.
MongoDB
Cassandra
Best used: If you need dynamic queries. If you prefer to define indexes, not map/reduce functions. If you Best used: When you write more than you read need good performance on a big DB. If you wanted (logging). If every component of the system must be CouchDB, but your data changes too much, filling up in Java. disks. For example: Banking, financial industry (though not For example: For most things that you would do with necessarily for financial transactions, but these industries are much bigger than that.) Writes are MySQL or PostgreSQL, but having predefined faster than reads, so one natural niche is real time columns really holds you back. data analysis.
Common Usage
Content Management System Real-time Analytics Page/Query Cache Logging and Archiving Event Messaging Job Queue Social Networking Data Mining & Warehousing Binary Storage (Files, Images,) Sessions MongoDB, CouchDB MongoDB, Cassandra Redis, Voldemort MongoDB, Cassandra Redis, Cassandra MongoDB, Redis Neo4J, Cassandra Hbase, InfiniDB MongoDB Redis, MongoDB
Bibliography
NoSQL Databases by Christof Strauch MongoDB the definition guide by Kristina Chodorow & Michael Dirolf MongoDB Official Site - www.mongodb.com http://www.slideshare.net Compare Sites:
http://kkovacs.eu/cassandra-vs-mongodb-vs-couchdb-vs-redis http://nosql.mypopescu.com/post/298557551/couchdb-vs-mongodb