Introduction to MongoDB | Part 2
MongoDB is one of the most popular NoSQL databases. It is an open-source document-oriented database. The term 'NoSQL' stands for 'not only SQL.' This implies that MongoDB isn't based on the table-like relational database structure but provides an altogether different data storage and retrieval mechanism. This format of storage is called BSON(Binary JSON).
This blog will discuss the important terms related to MongoDB, data storage in MongoDB, advantages of MongoDB, disadvantages of MongoDB, etc. Please check out the Introduction to MongoDB | Part 1 to get a better understanding of MongoDB.
How does MongoDB stores data?
It is easier to work with MongoDB than working with any relational database. MongoDB does not have any tables. Instead, all the data is stored in BSON format internally and over the network. The data is stored as key-value pairs where we define a unique key with a value associated with it. In MongoDB, we can store any type of data like integer, string, boolean, double, binary data, array, object, etc.
These key-value pairs are then stored in a document, which in turn is stored in a collection. Any number of documents can be stored in a collection, and the documents can have any number of key-value pairs stored in them.
A collection can be thought of as equivalent to a table in a relational SQL database. It always exists in a database, and there is no pre-defined structure of a collection. In MongoDB, the database contains collections, and in SQL, the database contains tables.
Let us discuss some of the basic terms related to MongoDB.
A container for collections is called a database. Each database in the collections gets its own set of files on the file system. Typically, a single MongoDB server has multiple databases.
A collection is a group of documents. It exists within a single database. They do not enforce a schema, and the documents within a collection can have different fields.
It is a set of key-value pairs. Documents have a dynamic schema, i.e., the documents in the same collection need not have the same structure or set of fields, and common fields in a collection's documents may hold different types of data.
This field is required in every MongoDB document. It represents a unique value in the MongoDB document. The _id field acts as the document's primary key. If a new document is created without an _id field, MongoDB will automatically create the field.
It is the key-value pair in a document. A document can have zero or more fields. Here, BSON stores data in key-value pairs.
Let's compare the relationship of MongoDB terminology with RDBMS.
|Embedded Documents||Table Join|
|Primary Key (Default key _id provided by MongoDB)||Primary Key|
MongoDB supports many data types. Some of them are as follows:
- Integer − Used to store a numerical value. The integer can be 32 bit or 64 bit depending upon the server.
- String − Most commonly used datatype to store the data. The string must be UTF-8 valid.
- Boolean − Used to store a boolean value.
- Double − Used to store floating-point values.
- Object − Used for embedded documents.
- Arrays − Used to store arrays/list/multiple values into one key.
- Min/ Max keys − Used to compare a value against the lowest and highest BSON elements.
- Timestamp − Used to record when a document has been modified or added.
- Date − Used to store the current date or time in UNIX time format. We can specify our own date time by creating an object of Date and passing day, month, year into it.
- Null − Used to store a Null value.
- Object ID − Used to store the document's ID.
- Binary data − Used to store binary data.
- Regular expression − Used to store regular expression.
Data modeling in MongoDB
While modeling data in MongoDB, we have to keep the following points in mind:
- Collect the business needs of the application and see what data and the type of data are needed for the application. Based on this, decide the structure of the document.
- If there is heavy query usage, consider the use of indexes in the data model to improve the efficiency of queries.
- In some scenarios, frequent inserts, updates, and removals happen in the database. In those cases, reconsider using indexes or incorporate sharding if required in the data modeling design to improve the efficiency of the overall MongoDB environment.
RDBMS vs. MongoDB
MongoDB and relational databases are different in various aspects. A few key differences between both are discussed below.
|Tables, schemas, and relations have to be created first.||No need to create such tables, schema, and relations.|
|To join two tables, we have to use joins that are a bit complex.||There are no joins. To change the document's structure, we have to embed a document inside another.|
|The primary key has to be set.||It automatically provides an _id field which acts as the primary key.|
|Set up is not easy.||Set up is easy.|
|Not suitable for hierarchical data storage.||Best for hierarchical data storage|
|Complex transactions are supported.||Complex transactions are not supported.|
|The document structure is not flexible and scalable.||Allows a highly scalable and flexible document structure.|
|Requires the data to be normalized first to prevent orphan records and duplicates.||It does not require the data to be normalized.|
Where to use MongoDB?
MongoDB is preferred over RDBMS(Relational Database Management System) in the following situations:
- Adding a new column in RDBMS is complicated. MongoDB is schema-less, and adding a new field does not affect old documents and will be easier.
- Different servers store multiple copies of data. Hence, data recovery is instant and safe even if there is a hardware failure.
- If a massive amount of data has to be stored in tables, MongoDB is preferred over RDBMS databases. MongoDB has a built-in solution for partitioning and sharding the database.
Advantages of MongoDB
The various advantages of MongoDB are as follows:-
- It is a schema-less database, i.e., it does not require a rigid, pre-defined schema like a relational database. The number of fields, content, and size of the document can differ from one document to another.
- Easily scalable.
- Set-up and installation are simpler in MongoDB.
- MongoDB has a simpler document query language when compared to SQL queries.
- Easier to store arrays and objects as MongoDB uses the BSON format to store data.
- It is free to use.
- The performance of MongoDB is much higher than compared to any relational database.
- Mapping application objects to database objects in MongoDB is not required.
- Faster access to the data, as MongoDB uses internal memory for storage.
- It gives us the flexibility and freedom to store data of different types.
- It has features like replication and gridFS, which help to increase data availability in MongoDB.
- It has advanced features for ad hoc queries.
Disadvantages of MongoDB
Some of the disadvantages of MongoDB are as follows:-
- It stores key names for each value pair. Also, there is data redundancy due to no functionality of joins, which increases unnecessary usage of memory.
- There is a limit for document size (16MB).
- No transaction support.
- Nesting of documents for more than 100 levels cannot be performed.
- Doesn't support joins like a relational database. However, the joins functionality can be utilized by coding it manually. This may slow execution and affect performance.
Frequently Asked Questions
1. In which language is MongoDB written?
Ans:- MongoDB is written in the C++ programming language.
2. Where is MongoDB used?
Ans:- MongoDB is widely used along with NodeJS frameworks, AngularJS, and ReactJS. It is also a core part of the MERN and the MEAN stack.
3. What is the difference between JSON and BSON?
BSON(Binary JSON) is a lightweight binary-encoded serialization format capable of representing any JSON-like document.
4. Which are the programming languages supported by MongoDB?
Ans:- MongoDB currently provides support for programming languages like Java, C, C++, C#, Perl, PHP, Python, Ruby, Go, etc.
5. What is the difference between MongoDB and mongoose?
Ans:- MongoDB is a document-oriented database management system that stores data in the form of BSON documents.
Mongoose is an ODM(Object Document Mapper) that allows developers to create and manage data in MongoDB conveniently.
This blog covered the important terms related to MongoDB, advantages of MongoDB, disadvantages of MongoDB, data modeling in MongoDB, etc.
We hope you found this blog useful. Liked the blog? Then feel free to upvote and share it.