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 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 Database Non SQL Database
Relational Database Non-Relational Database
Supports SQL Query language Supports JSON Query language
Table based Collection based and key-value pair
Row based Document based
Column based Field based
Support foreign key No support for foreign key
Support for triggers 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

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.

MongoDb

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

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.

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:

  • F

  • A

  • Q

MongoDB is an open-source, document-oriented database that stores data as documents instead of traditional rows and columns. These documents are stored in collections and use BSON, a binary representation of JSON-like data. Its flexible structure makes MongoDB suitable for applications where data can change or have different fields over time.

Relational databases organize data into tables, rows, and columns with a predefined schema, while MongoDB stores data as flexible documents within collections. MongoDB documents can contain different fields and structures when required, without following the same fixed table structure. This approach can be useful for applications that work with changing or semi-structured data.

MongoDB provides a flexible document structure that can represent application data in a format that's easy for developers to work with. It also supports indexes, rich queries, and aggregation features for working with and analyzing stored data. These features make MongoDB a useful option for many applications that need flexible data storage.

MongoDB can be a good choice for applications that handle semi-structured data or have requirements that may change during development. It's commonly used for e-commerce catalogs, content management systems, mobile apps, social platforms, geospatial applications, and inventory systems. For highly transactional applications with complex relationships, a relational database may be a better choice.

A MongoDB database contains collections, and collections contain individual documents where application data is stored. A collection is somewhat similar to a table in a relational database, while a document can be compared to a record. Unlike traditional database records, MongoDB documents can have different fields and structures within the same collection.

MongoDB indexes help the database find relevant documents without examining every document in a collection for each query. They can improve query performance, especially when applications frequently search or filter large amounts of data. Choosing the right fields for indexing is important because unnecessary indexes can also add storage and maintenance overhead.

MongoDB can be useful for applications that need flexible document-based storage and handle data that may change over time. Examples include e-commerce platforms, mobile applications, social networking systems, games, inventory management, and applications that work with geospatial or sensor data. Its document structure can also make it convenient to store application objects and related information together.

MongoDB provides different commands and operators for updating, replacing, and removing documents stored in collections. Developers can update specific fields using operators such as `$set`, `$unset`, `$push`, `$pull`, and `$inc`, or replace an entire document when needed. Documents can also be deleted by applying conditions that identify the records to be removed.

Want to Scale Your Business? Let’s Meet & Discuss!

Canada Flag

CANADA

30 Eglinton Ave W Mississauga, Ontario L5R 3E7

India Flag

INDIA

3rd floor Purusharth Plaza, Amin Marg, Rajkot, Gujarat. 360002

India Flag

INDIA

1116, Zion Z1, Sindhu Bhavan Marg, Nr. Maple County Road, Bodakdev, Ahmedabad, Gujarat. 380059

Get a Quote Now

Let's delve into a thorough understanding of your challenges and explore potential solutions together