MongoDB Basics: A beginner's guide

MongoDB Basics: A beginner's guide

If you are an aspiring MERN stack developer, you should learn MongoDB. It is one of the emerging database software, becoming popular for its flexibility and scalability and is a must-learn in 2023.

This blog is a beginner's tutorial on MongoDB. After reading this, you will have a basic understanding of MongoDB, and NoSQL databases and you will also create your first database and perform basic operations like creating, sorting, finding and deleting.

What is MongoDB?

MongoDB is a NoSQL database that uses the document-oriented data model to store data. Here, data is stored in the form of documents, which are of a JSON-like object format - called BSON(Binary JSON) - that have multiple key-value pairs. The BSON format is just JSON strings stored in the form of 0’s and 1’s which makes it lighter to store and transport, compared to JSON.

A MongoDB database consists of multiple collections and each collection has multiple documents. Each document represents an individual record of data.

For example, a document can contain all data about a blog post - including details about when it was published, the length of the blog, the name of the author etc. Each document has a unique identifier, with the name _id, of a special type ObjectID and is used to access individual documents of a collection, just like a primary key in a SQL table.

What are NoSQL Databases?

NoSQL databases do not use the relational model like SQL databases. There are no foreign keys or table joins involved. Data is stored in multiple formats, such as document object format, key-value pair format, and column-based format.

NoSQL databases are gaining popularity due to their scalability, which allows you to scale your database system horizontally by distributing the database to more servers or nodes, and their flexibility, which allows you to store unstructured and semi-structured data, such as images, video, and audio. This also makes these databases popular in the Big Data space. Most of these databases do not use SQL to access and manipulate data and have their own languages.

Basic Commands and Operations

Here, I will be working with MongoDB compass on my computer.

This is what MongoDB Compass looks like. On the left side, you will be able to the available databases. There will be some default databases available, which we don’t have to worry about.

To create a database, we are going to click on the + icon.

We are going to create a Movie Database. So I’m setting the name as MovieDB with a new collection named “Movies”. Inside the Movies collection is where we are going to store the documents.

After creating the database, it gets added to the Databases section on the left side.

To add documents to our collection, we can use the Add Data button. But, I’m going to use the shell to write commands to add data.

In the shell below, we enter the following command

use MovieDB

To add data, we can use the db.collection.insertOne()command. We can pass the data as parameters to the method.

Here, we have to specify the name of the collection. So, the command will look like this

db.Movies.insertOne()

After inserting the data, we will get the acknowledgement in the shell itself. To view changes in the Collection, click the refresh button on the right side of the screen below the Find button.

To insert multiple documents in a single go, we can use

db.collection.insertMany()

Here, the parameter must be an array. So, the command will look like this

db.Movies.insertMany([{"title":"The Batman","Yearofrelease":"2022"},{"title":"Avengers:Endgame","Yearofrelease":"2019"}])

To find data, we can use

db.collection.find()

This prints the first 20 documents in the collection. To display the remaining data, we can use the it command in the shell.

To find a specific document, we can pass the search query as the parameter. For example, if we want to search for a movie that released in 2022, we can use the following command

db.collection.find({'Yearofrelease':2022})

To sort our data, we can use

db.collection.find().sort({field:(1/-1)}) command

Here, field is what we want our data to be sorted by. -1 and 1 are used to indicate descending and ascending order respectively. For example, if we want to sort our movie data based on the year of release, we can use

db.Movies.find().sort({'Yearofrelease':1})

To get the size of the entire collection, we can use the find() method and the command is

db.collection.find().count()

We can add fields based on which we can find the data and find the count of those selected documents. We just have to add the field name and the property in the find() method.

We can also find one document based on a search field. To do so, we can use

db.collection.findOne({field})

To delete one document ,we can use the deleteOne() method where we must specify a particular field. Here, I’m specifying the title of the movie. So the command will look like this

db.Movies.deleteOne({”title”:”The Batman”})

And, we will get an acknowledgement just how we got for insertion.

To delete many, we can use the deleteMany method and we must specify the field here as well. For example, we can delete movies that we released before 2010.

Here, we will be using operators. We will be using the lesser than operator. To delete movies released before 2010, we are going to use the command

db.Movies.deleteMany({”Yearofrelease”:{$lt:"2010"}})

$lt is the lesser than operator.

Outro:

Hope you learnt something new from this blog and tried creating your first database in MongoDB.

If you have any feedback on the blog, feel free to share it with me and Follow for more blogs like this one. Subscribe to the newsletter so you don’t miss out on any new blogs I’m writing.

Thanks for reading!! See you soon!

Images conceptualized and designed by Athithya. (Not the screenshots ofc :))