How to work with MongoDB?

Introduction

MongoDB is a document-based database that is categorized as a No-SQL database. It supplies the data in the form of JSON documents. That is with an auto-generated recognition for every document.

Unlike structured SQL databases, the table structure is not fixed in a No-SQL database. MongoDB saves the data in the form of JSON string regardless of the count of traits or names of traits in a particular column.

This permits the developers to quickly make changes in the objects deprived of the requirement for any changes in the database level. In this post, we will learn about the fundamentals of MongoDB.

Description

MongoDB was made for speed. The data is built on BSON documents. That permits it to be that far quicker in calculating to find data in documents. MongoDB calls the denormalization of the data in its documents in order to be even well-organized in its requests. MongoDB inspires denormalization by repeating the data where it is demanded.

The data in a MongoDB assembly may be totally varied. This is known as the Schemaless. The benefit of not essentially having a severe data structure is to be able to rapidly change its data structure. This flexibility is importantly valued in projects at the prototype stage. Those are still realizing how their data should be structured.

One of the important concepts of MongoDB is to continuously have more than one copy of the database accessible. That is in order to confirm each time fast availability even surprisingly of failure of the host machine. This capability to duplicate the database across many machines in many locations simply supports to development of the horizontal scalability of a database.

Installation

There are some methods to install MongoDB. The best operative way to do this is to use the MongoDB Database-as-a-Service present named MongoDB Atlas. This is a facility that we can access using our preferred web browser. This may be installed on all three main public cloud providers such as Amazon Web Services, Microsoft Azure, and Google Cloud Platform. They come with a free tier and use just a few clicks. We will have provisioned a totally operative MongoDB database.

  • We need to do is to create an account to start the setup on MongoDB Atlas.
  • Once we do, we require to create a project.
  • After that, create the actual MongoDB Cluster.
  • Now, choose the favored cloud provider, supply a name for the cluster, and click Create Cluster.

Let’s configure the security options after the cluster is created. We need to configure the two things: the IP Whitelist addresses and a database user.

  • Click on Add the Current IP Address and then select a username or password that we will remember for the IP whitelist.
  • On the next page, choose the Connect with the mongo shell option.
  • We may also use the official GUI client for MongoDB named Compass to connect to the MongoDB cluster.
  • We may download MongoDB Compass from the MongoDB downloads page.
  • We can connect to the MongoDB Atlas cluster by providing the hostname from the connection string once we’ve installed the application.
  • That is from the above window in addition to the username and password.
  • Click on ’Fill in connection details individually and then Connect once we open Compass.

Working with MongoDB

We are now ready to start exploring MongoDB’s abilities as soon as we have a MongoDB database provisioned for us from MongoDB Atlas. We have a few options for how to work together with the database that just created provisional on our favorites.

  • Over the official cross-platform graphical user interface for MongoDB, Compass.
  • Via our web browser and the MongoDB Atlas web user interface.
  • This boundary is very similar to MongoDB Compass.
  • By using a command-line tool – the mongo shell.
  • With the help of a programming language such as API, or an IDE.
  • Also included VS Code extension, Python MongoDB driver, node.js MongoDB driver, Java MongoDB driver.
  • This is the best idea to work with the MongoDB Atlas web UI for explanatory jobs.
  • However, moving to a programming language is going to be a good option since it will provide us with better control.
  • MongoDB doesn’t need to download any additional drivers or net connection strings as using MongoDB Atlas is the easiest solution.

Making first MongoDB collection

  • The GUI will guide us to do so if we are using MongoDB Atlas.
  • For instance, we chose for using the mongo shell
  • Making a collection is as easy as opening a command line linking to mongo, and performing the next command: \
$ > db.createCollection(‘myFirstCollection’);
  • Make the JSON document on the fly using the insertOne command.
  • From the same command line, run:
$ > db.myFirstCollection.insertOne ({firstName: ‘John’, surname: ‘Doe’, department: ‘HR’});
  • MongoDB will automatically stock documents in binary format.
  • That importantly speeds up the recovery and access times.
  • This format is named BSON, also known as Binary JSON.
  • Furthermore, MongoDB reserves a field called _id for every collection.
  • This field is the main key for any collection and exclusively identifies a document inside any collection.
  • If we don’t identify an _id during inserting a document in a collection, MongoDB would fill that with Mongo ObjectId value.
  • We may identify custom primary keys with one or more values of choice.

Filtering records in MongoDB

  • It is likely to filter exact records from a MongoDB collection.
  • MongoDB helps all the common filter operators with similar numerical and string querying comparisons. For instance:
db.inventory.find( { qty: { $gt: 20 } } )
  • We are filtering records from the inventory collection to return only documents in this query.
  • That is when their qty data field is greater than 20, using the $gt comparison query operator.

In the same way:

db.inventory.find( { qty: { $in: [ 5, 15 ] } } )
  • We are filtering records from the alike collection to return only documents when their qty data field is equal to 5 or 15, using the $in comparison query operator.