MongoDB is a non-relational, document based database.
Relational databases such as MySql and Oracle store data in tables, rows and columns. They are based on a branch of algebraic theory known as relational algebra. Relational databases are structured, and tables can be linked with each other via foreign keys. Relational databases follow ACID properties, which ensures that database transactions are processed reliably.
Non-Relational databases, also called NoSQL databases, contain unstructured data and are commonly used in big data solutions to store and process massive amounts of disparate data.
There are four different kinds of NoSQL databases.
Graph databases – Graph databases are based on graph theory. These databases are designed for data which needs to be represented as graphs. The data elements are interconnected with multiple number of relations between them. Example of a graph database is Neo4j.
Key-Value stores – These databases store data as an indexed key and value pairs. These databases store data in a schema-less way. Example of key-value data stores include Cassandra, DynamoDB, Riak and BerkleyDB.
Column store – These databases are designed to store data as columns of data, rather than as rows as data. Example of column store databases are HBase, BigTable and HyperTable.
Document databases – Document databases are designed to store documents, with each document having a unique key. Examples of document databases are MongoDB and CouchDB.
Following are the key features of MongoDB.
Document store - MongoDB is non-relational document based database. Documents in MongoDB contain field and value pairs and are structured similar to JSON objects. Fields in MongoDB documents may contain other documents, reference to other document, arrays and arrays to other documents.
High Availability – MongoDB provides high availability as part of its core functionality. MongoDB has replica sets that replicates data and provides high availability and data redundancy.
Horizontal scalability – MongoDB provides sharding feature as part of its core functionality which distributes data across a cluster of machines.
Query Language – MongoDB provides a rich query language that supports read and write operations, aggregations and search operations.
MongoDB database contains Collections. Collections contains Documents. Documents contains fields and values in BSON format.
Collections are analogous to tables in relational database. Documents are analogous to rows in relational database.
You can create a new MongoDB database via Mongo shell by using the command ‘use’ followed by the database name
> use interview_grid_db
switched to db interview_grid_db
Explicit creation – You can explicitly create a new collection by using the command db.createCollection(). This enables us to set properties on the collection such as the setting the maximum file size, validation rules etc.
Implicit creation – MongoDB creates a new collection automatically, if you insert a document into a collection and that collection does not exist. In below example a new collection ‘employees’ is created if the collection does not already exist.
//Explicit Creation
>db.createCollection("employees")
{ "ok" : 1 }
//Implicit Creation
>db.employees.insert({fname:"John", lname:"Doe", age:"25",
title:"Manager", dept:"IT"})
WriteResult({ "nInserted" : 1 })
Capped collections are collections that store a fixed number of documents and maintains the insertion order of the documents.
If the number of documents in a capped collection reached the maximum, then the earliest inserted document will be deleted to make space for the new document.
Mongo shell is a command line user interface to MongoDB. You can use Mongo shell to query and update data from MongoDB. MongoDB is written in Java script.
Mongo shell is a command line user interface to MongoDB. You can use Mongo shell to query and update data from MongoDB. MongoDB is written in Java script.
You can start mongo shell by going to