MONGO DB In Practice

Royal Cyber Inc.
5 min readAug 3, 2022

--

MongoDB is a non-tabular database that uses documents to store data. It enhances the performance of an organization by providing schema-free maneuverability, read and write scalability, and sharding in terms of data management. Since data is stored in the form of documents, MongoDB brings increased consistency and partition tolerance.

Dive into this blog to get a glimpse of how you can work with MongoDB database.

How Does MongoDB Work?

MongoDB database transforms the data into binary format to store it. For this purpose, it uses a specific language called BSON. The data-carrying documents are arranged in collections. And the distribution of data in collections is determined by the shard key, i.e., an indexed field. Finally, the data is held in the form of either fields or values. In laymen’s language, fields can be thought of as categories or nouns, and values can be referred to as variables.

MongoDB Architecture

You can follow the simple steps given below to get the gist of how to tread in MongoDB smoothly.

Mongo shell has started, and the command “show dbs” is showing all the databases.

The keyword “use” is used to switch to a specific database.

You can create a collection named “items” by using the “createCollection” command.

The keyword “show collections” is used to show what collections or tables (in the case of RDBMS) are present in a specific database.

If you want to drop any collection, you can use, e.g., “db.items.drop().

To insert a single document or record (in the case of RDBMS), you can use the “insert” keyword.

To insert multiple documents in a collection, you can use the keyword “insertMany” along with the values to be inserted.

Moreover, to find out which documents are present inside a specific collection, you can use, e.g., “db.students.find().

If you would like to look at all the documents in a nicer and more organized manner, you can add the keyword “pretty” with the previous command, e.g., “db.students.find().pretty()”.

The first line of code is to look at a specific number of documents within a collection by using the keyword “limit.”

The second line of code serves the purpose of finding out the total count of documents within a collection.

The third line of code is used to sort the documents on a specific category in ascending order.

This line of code is meant to sort the documents based on a category in descending order.

Here, the first line of code is used to perform an update operation in a document by changing the name from “Harry” to “Mary.”

The second line of code is used to find out all the students whose id is greater than 10.

Finally, the third line of code is used to find out all the students whose id is smaller than 10.

Best Practices in MongoDB

Now that you know how MongoDB basically operates, let’s see how you can make the most of this database. The following tips will help you use your MongoDB to the maximum.

Know your query patterns

To build optimal data models, it is important that you understand the query patterns of your application. You can ultimately use this understanding to form suitable indices and reduce read load on the database. Moreover, you can also form subdocuments to do efficient data modeling.

Use Sharding & Replication to boost scaling

One can perform replication and sharding to gain horizontal scaling. Replication is done through replica set formation, which lets you balance your load by duplicating the same data. Through replication, queries can also be done on secondary nodes. One can also do sharding to partition databases into smaller parts to increase availability.

Perform embedding

MongoDB database lets you construct embedded or nested documents. Embedding means forming documents within documents. There is no need to store data separately if it is related. Instead, you can save the data under one document and save yourself the time and effort multiple queries and updates take.

Utilize the Indexing feature

In simple language, indexing means creating categories for data. It is a structure that keeps specific data stored in it. This feature makes it easier to retrieve that particular data when a need arises. In other words, when a query is run, the whole document is not perused. Instead, only particular areas are searched to collect the needed data.

Choose the right storage engine

MongoDB database incorporates multiple storage engines that are suited to meet different levels of workloads. Storage engines determine how the data is stored in both disc and memory. Therefore, it is essential that one selects the right type to get their job done effectively.

Conclusion

MongoDB Database is known for its scalability and agility. By holding nonrelational data, it lets data engineers, scientists, and BI personnel put it to use efficiently. In addition, MongoDB enables users to access data easily and whenever they want to.

Author bio:

Hassan Sherwani is the Head of Data Analytics and Data Science working at Royal Cyber. He holds a PhD in IT and data analytics and has acquired a decade worth experience in the IT industry, startups and Academia. Hassan is also obtaining hands-on experience in Machine (Deep) learning for energy, retail, banking, law, telecom, and automotive sectors as part of his professional development endeavors.

--

--

Royal Cyber Inc.

Royal Cyber Inc is one of North America’s leading technology solutions provider based in Naperville IL. We have grown and transformed over the past 20+ years.