Getting Started with MongoDB

MongoDB is a pretty awesome NoSQL database that has become super popular in the past few years. I’ve recently started learning MongoDB and I have to say I can understand it’s popularity. Modern applications are rapidly evolving and many will argue that relational databases are not designed to deal with the challenges that have presented themselves in modern development. MongoDB addresses issues like scalability, speed, and performance and it was built specifically for managing massive amounts of rapidly changing structured, semi-structured, unstructured and polymorphic data.

The MERN and MEAN stacks are probably the most notable stacks using MongoDB and it’s really no secret why so many developers have fallen in love with these technologies. The entire stack is tied together by JavaScript and it’s successors (ES6, JSX, TypeScript, etc) along with good old JSON. This enables developers to write JavaScript and use JSON on the front-end (React/Angular), the back-end (Node.js), and in the database since MongoDB is a document database. It’s truly a powerful combination and most developers/systems engineers have found that using these technologies together will often streamline the development lifecycle and can even shorten it. If you are interested in learning more about Mongo and modern web development you can check out their website and their blog, there is a lot of great information there.

Now, let’s go over how to get MongoDB installed on your computer and get a local database up and running. I will be going over how to install Mongo on Windows, but the process is really similar across the major operating systems (Windows, Linux, Mac). Getting the database started is actually really simple and it just requires 3 easy steps.

1. Download + Install

First download and run the MongoDB installer for the latest stable release from here. Make a note of the installation path for Mongo (specifically the bin folder), if you choose the default installation path, you should find the bin folder at some path like C:\Program Files\MongoDB\Server\3.6\bin. You can open up a command prompt and cd to that directory once the installation is complete.

2. Create the Data Directory

Now that MongoDB is installed you can create the data directory which should like something like C:\data\db. By default, Mongo looks for these two files inside the root of the path that it was installed on (the C: path for most Windows users) but you can place the directory anywhere on your computer. Just note that if you put the files elsewhere you will need to let Mongo know when you start the server.

3. Start the Database

Now we can start the database by running mongod from the command prompt that you opened in the first step (inside C:\Program Files\MongoDB\Server\3.6\bin). If you put your data directory somewhere besides the absolute path of the drive from which you installed MongoDB, then you can run mongod --dbpath d:\data to start the database and d:\data represents the location of your data directory. Another option is to specify the data directory path inside a config file and you can read more about that here.

After running mongod you should see a bunch of text output to your command prompt and at the end, you will see [initandlisten] waiting for connections on port 27017. Now your local MongoDB database is up and running on port 27017 which is the default port for Mongo. That’s it! Pretty simple huh? Connecting the database to a Node.js backend and building out some data models to play with is actually pretty simple too, especially if you are already familiar with JavaScript and JSON. So in my next post, I will go over how to connect a Node.js express back-end to our local MongoDB server.

Leave a Reply