MongoDB is a leading NoSQL database and an open source document database. It is primarily written in C++. This article will give you great understanding on MongoDB concepts in brief. This will include concepts which are needed to create and deploy a highly scalable and performance oriented database.
MongoDB works on various terminologies which are as follows:
1. Database: – A single MongoDB server consists of various databases where each database is a physical container of collections.
2. Collection: – A collection in MongoDB is equivalent to a database table and it exists within a single database. It includes a group of MongoDB documents.
3. Document: – Document can be defined as an instance of a MongoDB collection. It includes a set of key value pairs. All documents include a dynamic schema which means documents which comprise of same collection do not need to have same set of fields and structure.
Now that we’ve had an overview about MongoDB. It is also important to look at the pros and cons of MongoDB.
1. MongoDB is a schema-free database. It does not follow a typical schema design as in relational database management system where it shows a number of tables and relationships between these tables. In MongoDB, there is no concept of relationship.
2. MongoDB is easy to scale.
3. It uses internal memory which enables faster access to the data.
4. Structure of object designed is crystal clear.
1. It provides less flexibility with querying.
2. No support for transactions.
3. No structure of table designed. Logic is associated with document in JSON format.
Where to use MongoDB?
1. Big data
2. Mobile Infrastructure development.
3. Data Hub and User Data Management.
Installation (Environmental Setup):
Considering Windows as the operating system which is used commonly by developers and other users.
For installing MongoDB in a windows operating system, the following steps are needed to be implemented:
1. Download Enterprise server edition of MongoDB. The link for download is as follows:
After downloading the msi file, install the executable in your system. Once the installation is completed, it is vital to add the path of MongoDB in environment variables of your system. Once it is added, restart your system.
After restarting the system, create a folder named “Data” in the associated drive where MongoDB is installed. This will include all the database dump.
To start the configuration,
1. Use the command prompt with the command “mongod” to start with the listener.
2. To use MongoDB run the command: mongo. This will connect you to the MongoDB instance.
To get the list of commands, type db.help() in command prompt to give list of commands as shown in the screenshot.
The command db.stats() will give the complete statistics of MongoDB server.
The statistics will display the database name, collections and documents associated with them.
Following operations are used for the basic understanding of CRUD operation.
1. MongoDB uses Database_Name to create a database. The command will create a new database if the database does not exist, and uses the database if it exists.
2. MongoDB creates collection in place of tables. It uses db.createCollection for creating a new collection in database.
3. The basic command for inserting data into the MongoDB collection, is as follows:
4. Querying the data in the MongoDB collection is done with the find() command. The basic syntax for querying the data is as follows:
5. MongoDB uses update() and save() command for updating a document into a collection. The update() method updates the value in the document while the save() method on the other hand replaces the existing document.
6. MongoDB uses the remove() method to remove a document from collection.
7. Sometimes, it is important to remove a collection from the database. It is possible with following command as follows:
8. The following command is used for dropping database:
Sharding can be defined as the process of storing data records with multiple machines. It is a specific MongoDB approach to meeting the demands of growing data.
1. As the size of data increases, a single system may be not scalable to store the data nor provide an acceptable read/write throughout.
2. Sharding solves this problem of scalability.
3. With the process of Sharding, user adds more machines to support data growth and meet the demands of read and write operations.
1. Shards – They provide storage of data. It provides high availability and consistency. In a production environment, a particular shard can be considered as a different replica set.
2. Config Servers – It stores the cluster’s metadata. This data includes a mapping of the data sets to the shards. The query router uses this data to target operations specific to shards. Sharded clusters have exactly 3 config servers.
3. Query routers – Query routers are the database instances which basically provide interfaces with client applications and detect operations specific to a given shard.
A client interacts with a query router which communicates with shards and config servers.
Usage of MongoDB in web applications
1. For any web applications which are designed in scripting languages like PHP, Python or any other language it supports document oriented database along with relational database management system.
2. With the requirement analysis, we often figure out that database scaling is going to be a bigger issue. In such a situation, MongoDB provides a better solution of Sharding which helps to create replica sets for better performance.
3. They are better suited for document- based storage i.e. non-structured storage.
4. There is no hard-coded schema design needed which helps the performance increase of web application.