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 lead 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
Terminologies | Description |
Database | It 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. |
Collection | It 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 Collection | Capped Collections are fixed size collections. This means that the older records are dropped out after the limit is reached. |
Document | A 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 Databse | Non SQL Database |
Relational Database | Non-Relational Database |
Supports SQL Query language | Supports JSON Query language |
Table based | Collection based and key0-value pair |
Row based | Document based |
Column based | Field based |
Support foreign key | No support for foreign key |
Support for trgiggers | No support for triggers |
Contains schema which is pre-defined | Contains 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.
[cc lang=”C#”]> db.posts.find({tags: ‘MongoDB’});[/cc]
Find posts by author’s comments.
[cc lang=”C#”]> db.posts.find({‘comments.author’: ‘Johnson’}).count();[/cc]
Find posts written after 31st March.
[cc lang=”C#”]> db.posts.find({‘timestamp’: {‘$gte’: Date(’31-03-12’)}});[/cc]
Find posts which have not ‘common’ title.
[cc lang=”C#”]> db.posts.find({‘title’: {‘$ne: “common”});[/cc]
Other operators: $gt, $lt, $gte, $lte, $ne, $all, $in, $nin
How to update or replace the document
[cc lang=”C#”]> db.posts.update(
{“_id” : ObjectId(“5654381f37f63ffc4ebf1964″)},
{
title:”Test”
});
[/cc]
This will replace the document by {title:”Test”}
How to update or change only a part of the document
[cc lang=”C#”]> db.posts.update(
{“_id” : ObjectId(“5654381f37f63ffc4ebf1964″)},
{
$addToSet: {tags:”JS”},
$set: {title:”NodeJS server”},
$unset: { comments: 1}
});
[/cc]
Other operators: $set, $unset, $push, $pull, $pop, $addToSet, $inc, $decr and many more.
How to remove the document?
[cc lang=”C#”]> db.posts.remove( { title: “Test” }, true )[/cc]
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: