One of the common challenges that every growing business faces are the ability to efficiently handle the exponentially growing data. Apart from the Traditional Relational Databases, organizations are now using Document-oriented Open-source NoSQL Databases.
There are several NoSQL databases out there, but MongoDB is the most commonly used, and it is available both as a Cloud Service and for Deployment on Self-Managed Systems.
In this article, you will gain information about MongoDB Storage. You will also gain a holistic understanding of MongoDB, its key features, JSON, and the procedure of structuring data in MongoDB Storage.
What is MongoDB?
MongoDB is a NoSQL database that was developed by MongoDB inc, which is schema-free. It was designed and created using c++ and javascript allowing for higher connectivity. It uses a collection of Documents and has an option for creating schemas as well. It doesn’t follow the same structure of a traditional database wherein the data is stored in form of rows.
MongoDB uses Binary JSON and MQL as an alternative to SQL. BSON allows for data types such as the floating-point, long, date, and many more that are not supported by regular JSON.
MQL offers additional capabilities when compared to regular SQL making it more relevant for MongoDB as it processes JSON-type documents.
MongoDB is a NoSQL Server in which data is stored in BSON (Binary JSON) documents and each document is essentially built on a key-value pair structure. As MongoDB easily stores schemaless data, make it appropriate for capturing data whose structure is not known.
Key Features of MongoDB
Main features of MongoDB which make it unique are:
1) High Performance
Data operations on MongoDB are fast and easy because of their NoSQL nature. Data can be quickly stored, manipulated, and retrieved without any compromise on data integrity.
2) Scalability
In the Big Data era, MongoDB data can be distributed across a cluster of machines quickly and equally, free of bulkiness. The scalability of MongoDB handles a growing amount of data capably. Sharding is a process in MongoDB used to horizontally scale the data across multiple servers when the size of data increases.
3) Availability
Data is highly available with MongoDB as it makes multiple copies of the same data and sends copies of data across different servers. In case any server fails, data can be retrieved from another server without delay.
4) Flexibility
MongoDB can easily be combined with different Database Management Systems, both SQL and NoSQL types. Document-oriented structure makes MongoDB schema dynamically flexible and different types of data can be easily stored and manipulated.
What is JSON?
JSON, or JavaScript Object Notation, is a simple, readable data structure format. As an alternative to XML, it is primarily used to transmit data between a server and a web application. Squarespace stores and organises site content created with the CMS using JSON.
JSON is made up of two main components: keys and values. They form a key/value pair when combined.
- Key: A key is a string which is enclosed in quotation marks.
- Value: A value can be a string, a number, a boolean expression, an array, or an object.
- Key/Value Pair: A key value pair has a specific syntax, with the key coming first, followed by a colon, and then the value. Key/value pairs are separated by commas.
For example:
"choco" : "bar"
This example is a key/value pair. The key is “choco” and the value is “bar“.
Structuring Data in MongoDB Storage
The procedure for structuring data in MongoDB Storage are as follows:
1) Define Your Data Set
The first step in creating a MongoDB data store is to answer the question, “What kind of data do you want to store, and how do the fields relate to each other?”
The example taken in this article uses an inventory database to track items & their quantities, tags, ratings, and sizes.
Below is the example of the types of fields captured here.
name | quantity | size | status | tags | rating |
---|
journal | 25 | 14×21,cm | A | brown, lined | 9 |
notebook | 50 | 8.5×11,in | A | college-ruled,perforated | 8 |
paper | 100 | 8.5×11,in | D | watercolor | 10 |
planner | 75 | 22.85×30,cm | D | 2019 | 10 |
postcard | 45 | 10x,cm | D | double-sided,white | 2 |
2) Start Thinking in JSON
While a table may appear to be a good place to store data, as illustrated in the preceding example, there are fields in this data set that require multiple values and would be difficult to search or display if modelled in a single column in MongoDB Storage. For example, size and tags in the example considered.
You can solve this problem in an SQL database by creating a Relational table.
Documents are used to store data in MongoDB. These documents are saved in JSON (JavaScript Object Notation) format in MongoDB. JSON documents support embedded fields, allowing related data and data lists to be stored within the document rather than in an external table.
JSON is written in the form of name/value pairs. Fieldnames and values in JSON documents are separated by a colon, fieldname and value pairs by commas, and sets of fields are encapsulated in “curly braces” ({}).
If you wanted to start modelling one of the rows of the table in the example, such as:
name | quantity | size | status | tags | rating |
---|
notebook | 50 | 8.5×11,in | A | college-ruled,perforated | 8 |
This can be done with the name and quantity fields. These fields would look like this in JSON:
{"name": "notebook", "qty": 50}
3) Identify Candidates for Embedded Data and Model Your Data
Now, as you structure data in MongoDB storage, you must decide which fields require multiple values. These are candidates for embedded documents or lists/arrays of embedded documents within the document.
For example, in the preceding data, the size field could be composed of three fields:
{ "h": 11, "w": 8.5, "uom": "in" }
Some items have multiple ratings. So, the ratings field can be represented as a list of documents containing the field scores as illustrated below:
[ { "score": 8 }, { "score": 9 } ]
And you’d have to deal with multiple tags for each item. As a result, you may need to store them in a list as well such as:
[ "college-ruled", "perforated" ]
Finally, a JSON document that stores an inventory item might look like this:
{
"name": "notebook",
"qty": 50,
"rating": [ { "score": 8 }, { "score": 9 } ],
"size": { "height": 11, "width": 8.5, "unit": "in" },
"status": "A",
"tags": [ "college-ruled", "perforated"]
}
This looks very different from the tabular data structure you started with in Step 1.
For further information on efficient structuring of data in MongoDB storage.
Conclusion
In this article, you have learned about MongoDB Storage. This article also provided information on MongoDB, its key features, JSON, and the procedure of structuring data in MongoDB Storage in detail.
For further information on MongoDB Replica Set Configuration, MongoDB Compass Windows Installation, MongoDB Count Method, you can visit the following links.
Manisha Jena is a data analyst with over three years of experience in the data industry and is well-versed with advanced data tools such as Snowflake, Looker Studio, and Google BigQuery. She is an alumna of NIT Rourkela and excels in extracting critical insights from complex databases and enhancing data visualization through comprehensive dashboards. Manisha has authored over a hundred articles on diverse topics related to data engineering, and loves breaking down complex topics to help data practitioners solve their doubts related to data engineering.