An overview to Open Source Document Oriented Database – MongoDB


Relational DBMS has been around for quite a long time now. But Relational DBMS also has some disadvantages. To overcome the issues within RDBMS, MongoDB was built.

What is MongoDB?

Most of the developing platforms work with documents or objects. And to persist these documents or objects we need a database. Then, why not store the objects directly to the database? This thought leads to the idea of the creation of MongoDB.

“MongoDB is an open source document-oriented database with scalability and flexibility you need with the querying and indexing that you need.”


Few basic terminologies

TerminologiesDescription
DatabaseIt is made up of multiple collections. Each database has its own set of files on the file system. And a single MongoDB server has multiple databases on it.
CollectionIt can be compared to a table in SQL database. It is schema-less and contains documents and can be indexed by one or more keys.
Capped CollectionCapped Collections are fixed size collections. This means that the older records are dropped out after the limit is reached.
DocumentA document can be thought of as a record or row in SQL database. A Document is a set of key-value pairs and is stored in a collection. They have dynamic schema and stores the data in BSON (Binary form of JSON). Further more, documents also have id key that works similar to a primary key in MySQL.

Why use MongoDB?

MongoDB exhibits many features which makes it superior from RDBMS:
  • As we all now know, the biggest advantage of using MongoDB is it being a document-oriented database. This allows it to store data in document or object form.
  • It fully supports indexing thereby increasing the speed and improving the performance.
  • The queries are in rich document format which accounts for easy readability.
  • MongoDB allows the use of Map/Reduce for aggregation. It is similar to the GROUP BY query in SQL database.

When to use MongoDB?

MongoDB works great for:
  • E-commerce product catalog
  • Semi-structured content management
  • Mobile and social networking sites
  • Maintaining location-based data – Geospatial data
  • Database for sensor streams
  • Game development
  • Inventory management
However, don’t use it for a highly transactional system and tightly coupled system.

Difference between SQL and MongoDB

SQL DatabseNon SQL Database
Relational DatabaseNon-Relational Database
Supports SQL Query languageSupports JSON Query language
Table basedCollection based and key0-value pair
Row basedDocument based
Column basedField based
Support foreign keyNo support for foreign key
Support for trgiggersNo support for triggers
Contains schema which is pre-definedContains dynamic schema

Datatypes supported by MongoDB
Arrays, Objects, Regular expressions, Code, Binary data, Object ID and Min/max keys
MongoDB Support Platforms
Windows, Linux, OS X, Solaris
List of MongoDB GUI
RoboMango, Studio3T, RockMongo, MongoVUE, MongoHub, phpMoAdmin
Blog post in JSON DB



How to fetch results from MongoDB?
Find posts which has ‘MongoDB’ tag.
1
> db.posts.find({tags: ‘MongoDB’});</p>
Find posts by author’s comments.
1
> db.posts.find({‘comments.author: ‘Johnson’}).count();</p>
Find posts written after 31st March.
1
> db.posts.find({‘timestamp’: {‘$gte’: Date(31-03-12)}});</p>
Find posts which have not ‘common’ title.
1
> db.posts.find({‘title’: {‘$ne: “common”});</p>
Other operators: $gt, $lt, $gte, $lte, $ne, $all, $in, $nin

How to update or replace the document

1
2
3
4
5
> db.posts.update(
    {"_id" : ObjectId("5654381f37f63ffc4ebf1964")},
    {
        title:"Test"
    });
This will replace the document by {title:”Test”}

How to update or change only a part of the document

1
2
3
4
5
6
7
> db.posts.update(
    {"_id" : ObjectId("5654381f37f63ffc4ebf1964")},
    {
        $addToSet: {tags:"JS"},
        $set: {title:"NodeJS server"},
        $unset: { comments: 1}
    });
Other operators: $set, $unset, $push, $pull, $pop, $addToSet, $inc, $decr and many more.

How to remove the document?

1
>  db.posts.remove( { title: “Test” }true )
MongoDB is used by many enterprises like Disney, MTV, EA Sports, Craigslist, GitHub, Firebase, SAP, Saving Star, Bitly, The New York Times, The National Archives, IGN, Foursquare, Doodle, The Guardian, Source Forge and many others.

Install MongoDB Community Edition

Download the latest production release of MongoDB using given below link:
https://www.mongodb.com
1.Set up the MongoDB environment
MongoDB requires a data directory to store all data. MongoDB’s default data directory path is absolute path \data\db on the drive from which you start MongoDB. Create this folder by running the following command in command prompt.

You can specify an alternate path for data files using the –dbpath option to mongod.exe for example,


2.Start MongoDB
To start MongoDB, run mongod.exe. For example, from the command prompt


This starts the main MongoDB database process. The waiting for connections message in the console output indicates that the mongod.exe process is running successfully.
3.Verify that MongoDB has started successfully
Verify that MongoDB has started successfully by checking the process output for the following line:


The output should be visible in the terminal or shell window.
Studio3T editor looks like below:


Comments