MongoDB: The NoSQL Database

Nupur Sogani
7 min readMay 20, 2021

NoSQL databases i.e. Not Only SQL databases does not use tables to store data. They store data in different ways depending on the data model. Four major types of NoSQL databases are: document databases, key-value databases, wide-column stores, and graph databases.

MongoDB is a document database, which means it stores data in JSON (JavaScript Object Notation)-like documents. Each document contains pairs of fields and values and fields can vary from document to document. One document can have others embedded in it. It provides us flexibility as it is schema free. Every row is an independent document and combined together, it is known as a collection.

Example of a document :

Example of a Collection :

MongoDB provides CRUD operations that are used to create, insert, update and delete.

  • Create: Create operation is used to create a document. A document is directly inserted into the collection by using insertOne() and insertMany() methods. These methods create a new document that we specify and then insert them into a collection. The following is the syntax on inserting a document inside a collection.
  • Read : Read operation is used to retrieve documents from a collection. MongoDB provides find() method to retrieve data. The syntax is as follows.

db.collection-name.find()

The find() method has two parameters. The first parameter is the query and the second is the projection. If no parameter is given, it will retrieve every document in a collection.

1db.collection-name.find({"key" : "value"}, {"key1" : 1, "key2" : 0})

In the second parameter, the key with value 1 will appear in the result while the key with value 0 won’t.

To view the result in a more readable format, we can use pretty() method with find().

1db.collection-name.find({"key" : "value"}, {"key1" : 1, "key2" : 0}).pretty(()

  • Update : We may require to modify a document once it is inserted into a collection. The update operation is used for making changes in the existing database. MongoDB database provides two methods for update operations, updateOne() and updateMany(). The updateOne() updates a single document. It has three parameters. The first parameter is the filter(condition), the second one is the update itself while the third one consists of few options. The third parameter is optional.

To update a single document, we use the following syntax.

1db.collection-name.updateOne({"key" : "value"}, { $set : { "key1" : "value"}})

In the second parameter, we use a $set operator to set the update. There is a third parameter also. It has a few options. The most important and commonly used of these options is upsert. Whenever there is no matching document in the collection, the upsret option will add a new document which we specified in the update operation. But first, we have to add upsert as the third parameter and set it to true.

db.collection-name.updateOne({"key" : "value"},

{ $set : { "key1" : "value"}}, {upsert : true})

  • Delete : As the name suggests, Delete operation is used to delete documents from a collection. There are two method for delete operation, deleteOne() and deleteMany(). To delete a single document, we use the deleteOne() method.

db.collection-name.deleteOne({"key" : "value"})

The deleteOne() method has one parameter. This parameter specifies the filter. To delete multiple documents, we use deleteMany() method.

db.collection-name.deleteMany({"key" : "value"})

Advantages of MongoDB

  • Easy: Work with data in a natural, intuitive way
  • Fast: Get great performance without a lot of work
  • Flexible: Adapt and make changes quickly
  • Versatile: Supports a wide variety of data and queries
  • Availability: Deliver globally resilient apps through sophisticated replication and self-healing recovery
  • Scalability: Grow horizontally through native sharding
  • Workload Isolation: Run operational and analytical workloads in the same cluster
  • Locality: Place data on specific devices and in specific geographies for governance, class of service, and low-latency access
  • Portability: Database that runs the same everywhere
  • Cloud Agnostic: Leverage the benefits of a multi-cloud strategy with no lock-in
  • Global coverage: Available as a service in 50+ regions across the major public cloud providers

How to manage MongoDB ?

We can manage and use MongoDB in three ways:

  1. CLI : The most powerful and complete way to manage and configure MongoDB is through the command line interface (CLI). All of the functionality of MongoDB is available through the CLI which uses the mongo shell to enter commands and receive output.
  2. GUI : MongoDB Compass offers a GUI for those who prefer a visual interface. MongoDB Compass offers a way to visualize data, create indexes, and assemble complex aggregation pipelines that streamline the way we work with data. It helps us to view real-time server statistics and query performance.
  3. SDK : We can connect our applications to our database with official libraries for a number of languages like python, C, C#, Scala, Ruby, Node.js, Java, Go, PHP, Swift, Rust and many more.

MongoDB Clusters

In the context of MongoDB, “cluster” is the word usually used for either a replica set or a sharded cluster. In contrast to a single-server MongoDB database, a MongoDB cluster allows a MongoDB database to either horizontally scale across many servers with sharding, or to replicate data ensuring high availability with MongoDB replica sets, therefore enhancing the overall performance and reliability of the MongoDB cluster.

A replica set is the replication of a group of MongoDB servers that hold copies of the same data

A sharded cluster is also commonly known as horizontal scaling, where data is distributed across many servers.

MongoDB Atlas

It is a fully managed database service. It provides fully automated infrastructure provisioning, setup, and deployment. When requirements and workloads change, MongoDB Atlas makes it easy to make post-deployment modifications to the database. Scale up, add more storage, configure cross-region clusters, create read-only or analytics nodes, and more with the click of a button. Each self-healing Atlas cluster is distributed by design across the availability zones within your cloud region. It offers fully managed backup options, including incremental data recovery and cloud provider snapshots.

The great thing about managing MongoDB with MongoDB Atlas is that pretty much almost all of the time, we don’t have to worry about it. Not only can we make changes on the fly to our application leveraging MongoDB’s flexible data model, but we can deploy any downstream database changes on the fly or easily spin up new clusters to test new ideas. All of these can happen without impacting things in production; no worrying about provisioning infrastructure, setting up backups, monitoring, etc. It’s a real thing of beauty.

Ken Hoff, Developer Advocate, Stream

Companies that use MongoDB

MongoDB is used by Adobe, Buzzfeed, Comcast, Cisco, CodeAcademy, Craigslist, Ebay, EA, E-Harmony, Forbes, Google, Foursquare, IBM, Infosys, the United Kingdom Government, among many, many others.

Forbes : MongoDB Cloud Migration helped world’s biggest media brand continue to set standard for digital innovation. In just six months, Forbes migrated its platform to Google Cloud and MongoDB Atlas. Results include:

  • 58% faster build time for new products and fixes
  • Accelerated release cycle by 4x
  • Reduced total cost of ownership by 25%
  • 28% increase in subscriptions from new newsletters

During the pandemic the cloud infrastructure has also helped the website scale to an extraordinary number of users and helped the team stay nimble, introducing and testing a number of new features.

Toyota : To make platform more user-friendly and increase productivity of development team, Toyota shifted from a monolithic codebase towards a microservices approach. Not only because of data model but several other criteria were used to decide on which database platform they would use to make the switch:

  1. Performance
  2. Automatic scalability
  3. Security
  4. Data locality
  5. Automatic backups
  6. Cloud agnostic
  7. Developer friendly database
  8. Ecosystem for developer productivity

Conclusion

MongoDB is one of the most widely used NoSQL databases on the modern web. It is easy to understand. The document query language provides a lot of options and it is as powerful as the SQL. Unlike relational databases, MongoDB is easy to scale.

Resources :

  1. Acodez
  2. MongoDB

⚜ Thank you for reading ⚜

--

--