DEV Community

Cover image for Introduction To MongoDB and How To Use It
Boyan Iliev
Boyan Iliev

Posted on • Originally published at devdojo.com

Introduction To MongoDB and How To Use It

Introduction

MongoDB is a NoSQL (not only SQL) database. In NoSQL databases, we store our data in a collection of documents. These documents are very similar to JavaScript objects or JSON objects. So if you are familiar with JavaScript, then you are going to like Mongo, because it will be easier to get the hang of it.

MongoDB is very scalable. It has built-in replication and sharding. It's performant, it's fast, and mostly, it's very flexible. Because the data structure is like a JavaScript object, you don't have to map out your entire data structure beforehand as you do with relational databases. Unlike a relational database like MySQL, where you have to create all your tables and all your columns, Mongo gives you the freedom to structure your data however we want through our application. This doesn't mean it's better than relational databases, it all depends on how you need to use your database.

Installing MongoDB

We are going to install MongoDB through Homebrew. If you don't have Homebrew installed, just run the code below in your terminal and that will install Homebrew.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

Once you get Homebrew installed on your Mac, just run the three following commands. First, we run:

brew tap mongodb/brew
Enter fullscreen mode Exit fullscreen mode

Then run:

brew install mongodb-community@4.4
Enter fullscreen mode Exit fullscreen mode

Then after that, we will want to run it as a service with the following command:

brew services start mongodb-community@4.4
Enter fullscreen mode Exit fullscreen mode

And finally, we need to run the following command so that we can connect and start using MongoDB.

mongo
Enter fullscreen mode Exit fullscreen mode

This is what you should see after running the command:

Mongo - Imgur.png

Now that we have Mongo installed and ready, let's start with some of the main commands that you should know

Commands

Show Current Database

The command for seeing the current database is:

db
Enter fullscreen mode Exit fullscreen mode

Show All Databases

In order to see all of the databases just run the following command:

show dbs
Enter fullscreen mode Exit fullscreen mode

Switching and Creating Databases

If you want to switch which database you are using, just type in use followed by the database name.

use db_name
Enter fullscreen mode Exit fullscreen mode

If you want to create a DB, just type in the use command again, followed by your new DBs name.

use new_db_name
Enter fullscreen mode Exit fullscreen mode

If you run show dbs after creating a DB, the new DB won't show because it doesn't have anything in it, so don't worry when it doesn't show up after running the show dbs.

Dropping a Database

In order to drop a database, or in other words, delete it, you first need to switch to the database with the use command, and after that, you need to type in the following command:

db.dropDatabase()
Enter fullscreen mode Exit fullscreen mode

Creating and Viewing Collections

To create a collection in the DB that you are using, you just have to run the following command and set the name of your collection:

db.createCollection('collection_name')
Enter fullscreen mode Exit fullscreen mode

And if you want to view the collection, just run the show command followed by collections and that should print out the collections in your DB.

show collections
Enter fullscreen mode Exit fullscreen mode

Inserting Rows

Now in order to insert a row, just type in the db followed by the name of the collection that you want to insert the row in, and then by insert, which is then followed by parentheses(). And in those parentheses, you pass in an object with whatever you want.

Here is an example of how this should look like:

db.users.insert({
    username: 'Batman_fan_44',
    followers: 3,
    following: [
        'Alred',
        'Robin'
    ],
    status: {
        name: 'Bruce Wayne',
        status: 'active'
    }
})
Enter fullscreen mode Exit fullscreen mode

As you can see this looks just like a normal JavaScript object. You can store strings, numbers, arrays and can also have embedded objects inside. This is why Mongo is liked by so many, because of how similar it is to JS and how easy it is to use.

If you want to insert more than one row, instead of typing insert after the collections name, type in insertMany and then insert your rows in an array like this:

db.users.insertMany([
    {
        username: 'Tony Montana',
        followers: 1234
    },
    {
        username: 'Hannibal Lecter',
        followers: 5
    },
    {
        username: 'Rick Grimes',
        followers: 21
    }
])
Enter fullscreen mode Exit fullscreen mode

These rows don't have as many stuff like likes or status. which our first row has, but that doesn't matter. Unlike relational databases, in Mongo, we don't have that strict data model that we have to follow. We can put whatever we want wherever we want, which is awesome!

Viewing Rows

To view the rows of a certain collection, just run the following command.

db.collection_name.find()
Enter fullscreen mode Exit fullscreen mode

This will print out the data, but it will be hard to read. Luckily there is a method called pretty(), which formats all the rows.

db.collection_name.find().pretty()
Enter fullscreen mode Exit fullscreen mode

And now it will be much easier to read.

These are some of the main commands that you should know. I recommend that you check out this MongoDB Cheat Sheet and have a look at some other commands that might be useful to you:

https://gist.github.com/bradtraversy/f407d642bdc3b31681bc7e56d95485b6

Conclusion

As you can see MongoDB looks identical to JavaScript. They have similar syntax and are pretty easy to get the hang of.

If you want to have a MongoDB database cluster production-ready, I strongly recommend using the new DigitalOcean managed MongoDB clusters which you can deploy with just the click of a button so that you won't have to worry about server maintenance, which can be very challenging sometimes.

If you want you can use this referral code which gives you free $100 DigitalOcean credit.

If you also want to learn more about SQL and how to use it, I highly recommend that you check out this Introduction to SQL opensource ebook. It helped me understand how to use SQL and I highly recommend it.

I hope that this post has helped you get more comfortable with using MongoDB and getting the hang of it.

Latest comments (4)

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great post! Keep it up! 🚀

Collapse
 
boiliev profile image
Boyan Iliev

Thanks bro!

Collapse
 
kubeden profile image
Kuberdenis

Wow man, wasn’t expecting such a high-level post from you. Great work!!!!!

Collapse
 
boiliev profile image
Boyan Iliev

Thanks dude! Really appreciate it.