Mongo DB — The savior of backend

Vamsi Krishna Karanam
4 min readMay 22, 2020

Being a web/app developer, it’s of utmost importance for one to learn not only about designing the eye-catching, colorful user interfaces (front-end) but also equally or in some cases more importantly the backend technologies. And I’m sure that most of you might have come across SQL, MySQL, NoSQL, PostgreSQL (include everything SQL for that matter) with bold-lettered syntax, involving complex queries just to get a simple task done, as far as a full-stack development is concerned. If you’re like me, you are well versed and got used to the traditional programming syntax to a level where you might find understanding the DDL and DML challenging. This is where Mongo DB comes into the picture. Mongo DB, as it says in their official website, is a “document-based, general-purpose, distributed database built for modern application developers”. You might have already noticed why it is special.

Mongo DB is a document-based database, meaning it stores data in JSON format as shown below.

{   
"_id": "5cf0029caff5056591b0ce7d",
"firstname": "Vamsi Krishna",
"lastname": "Karanam",
"address": {
"street": "Balaji Nagar",
"city": "Tirumala",
"state": "AP",
"zip": "517504"
},
"hobbies": ["surfing", "coding"]
}

So as we see, we are eliminating the need for traditional tables with all those rows and columns involving complex queries to retrieve data. Mongo DB makes it much easier to perform the CRUD operations on the data stored in the database. Let’s see it in action.

First things first, to use Mongo DB in your application you need to install their server. You can either start free by downloading their community server or if you can afford a subscription you can download their enterprise server which will have some added benefits. For this tutorial we are just gonna go with the community server. Once you downloaded and installed it in auto settings that will pretty much be it.

Then create a folder data at the same directory in which Mongo DB is installed with another folder db inside it. (Important!)

That’s pretty much it and we are good to go. Now just to make sure we have installed everything properly open the command line and run the command

> mongo --version

If it returns the version of MongoDB shell along with some other parameters, congratulations!! You have successfully installed Mongo DB. If not, don’t get disappointed. 8 out of 10 unsuccessful installations is because of the missing environment variables. So create the required environment variables and assign them with mongo’s bin folder. If you don’t know how, be sure to check out this article. Now we can start our server by using the command :

> mongod

It might be overhead at first, but at the end if you see waiting for connection on port 27017, then the server is started. Now you might wanna leave this terminal as it is for the rest of the tutorial. You can also use a third party terminal like Hyper, which can support multiple tabs to avoid opening command prompt for 3 or 4 times again and again. Now open another tab or terminal and give the command :

> mongo

This command will start the mongo shell to interact with the database and will be waiting for the query. Now the Mongo DB query, unlike the traditional SQL, is a simple function call. First let us look at the existing databases. To do so give the command :

> show dbs

It will show the two default databases along with their sizes.

admin       0.000GB 
local 0.000GB

To create a new database or to use an existing database we can use the following query :

> use NewDatabase

which will return :

switched to db NewDatabase

To see which database you are currently in, use :

> db

Since Mongo DB uses documents to store data instead of tables, these documents are called collections. To view the collections inside a database, use the query :

> show collections

It will show the collections inside a database if any. Now, to use a new collection we can directly give the insert query which will also create the collection without an additional step.

> db.myNewFoodCollection.insertOne({
recipeName: 'Pav Bhaji',
category: 'Snack'
})

which will again return a document of acknowledgment:

{ 
"acknowledged" : true,
"insertedId" : ObjectId("5ec7ed74685a5bdcc007afbf")
}

To read a document we can use the find query, which looks something like this :

> db.myNewFoodCollection.find()
{ "_id" : ObjectId("5ec7ed74685a5bdcc007afbf"), "recipeName" : "Pav Bhaji", "category" : "Snack" }

Now to update a document, we can use the update query as follows :

> db.myNewFoodCollection.updateOne( 
{recipeName: "Pav Bhaji"},
{$set: {category: "All-time snack"}}
)
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }> db.myNewFoodCollection.find()

{ "_id" : ObjectId("5ec7ed74685a5bdcc007afbf"), "recipeName" : "Pav Bhaji", "category" : "All-time snack" }

Finally, to delete a document, we can use the delete query.

> db.myNewFoodCollection.deleteOne({_id:"5ec7ed74685a5bdcc007afbf"}){ "acknowledged" : true, "deletedCount" : 0 }

This pretty much sums it up the CRUD operations using Mongo DB shell. You can know more about other queries by reading their official documentation here. If you’ve reached here following all the steps, thanks for taking the time to give this a read, and also feel free to comment your questions/suggestions. In the next article I will share how MongoDB becomes a powerful tool when combined with a web framework like Node js.

--

--