MongoDB Main Components

MongoDB is a crossplatform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document.


Database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases


Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.


A document is a set of keyvalue pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.

Below given table shows the relationship of RDBMS terminology with MongoDB

Database Database
Table Collection
Tuple/Row Document
column Field
Table Join Embedded Documents
Primary Key Primary Key (Default key _id provided by mongodb itself)

Sample document

Below given example shows the document structure of a blog site which is simply a comma separated key value pair.

_id: ObjectId(87f78a12345c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'Java',
url: '',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100,
comments: [
			message: 'My first comment',
			dateCreated: new Date(2011,1,20,2,15),
			like: 0
			message: 'My second comments',
			dateCreated: new Date(2011,1,25,7,45),
			like: 5

_id is a 12 bytes hexadecimal number which assures the uniqueness of every document. You can provide _id while inserting the document. If you didn't provide then MongoDB provide a unique id for every document. These 12 bytes first 4 bytes for the current timestamp, next 3 bytes for machine id, next 2 bytes for process id of mongodb server and remaining 3 bytes are simple incremental value. Sample document Any relational database has a typical schema design that shows number of tables and the relationship between these tables. While in MongoDB there is no concept of relationship

MongoDB Features

  • Document-oriented: Instead of taking a business subject and breaking it up into multiple relational structures, MongoDB can store the business subject in the minimal number of documents. For example, instead of storing title and author information in two distinct relational structures, title, author, and other title-related information can all be stored in a single document called Book.
    Ad hoc queries MongoDB supports search by field, range queries, regular expression searches. Queries can return specific fields of documents and also include user-defined JavaScript functions.
  • Indexing: Any field in a MongoDB document can be indexed (indices in MongoDB are conceptually similar to those in RDBMSes). Secondary indices are also available.
  • Replication: MongoDB provides high availability with replica sets. A replica set consists of two or more copies of the data. Each replica set member may act in the role of primary or secondary replica at any time. The primary replica performs all writes and reads by default. Secondary replicas maintain a copy of the data of the primary using built-in replication. When a primary replica fails, the replica set automatically conducts an election process to determine which secondary should become the primary. Secondaries can also perform read operations, but the data is eventually consistent by default.
  • Load balancing: MongoDB scales horizontally using sharding. The user chooses a shard key, which determines how the data in a collection will be distributed. The data is split into ranges (based on the shard key) and distributed across multiple shards. (A shard is a master with one or more slaves.) MongoDB can run over multiple servers, balancing the load and/or duplicating data to keep the system up and running in case of hardware failure. Automatic configuration is easy to deploy, and new machines can be added to a running database.
  • File storage: MongoDB can be used as a file system, taking advantage of load balancing and data replication features over multiple machines for storing files. This function, called Grid File System, is included with MongoDB drivers and available for development languages (see "Language Support" for a list of supported languages). MongoDB exposes functions for file manipulation and content to developers. GridFS is used, for example, in plugins for NGINX and lighttpd. Instead of storing a file in a single document, GridFS divides a file into parts, or chunks, and stores each of those chunks as a separate document. In a multi-machine MongoDB system, files can be distributed and copied multiple times between machines transparently, thus effectively creating a load-balanced and fault-tolerant system.
  • Aggregation: MapReduce can be used for batch processing of data and aggregation operations. The aggregation framework enables users to obtain the kind of results for which theSQL GROUP BY clause is used.
  • Server-side JavaScript execution: JavaScript can be used in queries, aggregation functions (such as MapReduce), and sent directly to the database to be executed.
  • Capped collections: MongoDB supports fixed-size collections called capped collections. This type of collection maintains insertion order and, once the specified size has been reached, behaves like a circular queue.
  • Sharding: Sharding is the process of storing data records across multiple machines and is MongoDB's approach to meeting the demands of data growth. As the size of the data increases, a single machine may not be sufficient to store the data nor provide an acceptable read and write throughput. Sharding solves the problem with horizontal scaling