How to Setup MongoDB on Windows

2021/02/034 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Install MongoDB Server
  3. Install MongoDB Tools
  4. Install MongoDB Compass
  5. Create Database, Collection, and Record
  6. MongoDB Backup and Restore Data
  7. Conclusion

Introduction

This blog shows how to set up MongoDB 4.4 Community version on Windows 10 as a local development environment, create a simple database, table, and collection, and how to backup and restore data by using mongoexport and mongoimport.

Install MongoDB Server

First, go to the official MongoDB installation link here to download the installation file. After the download, follow the installation wizard to install MongoDB.

Choose Location

The default location is under the Windows C drive. You can modify the location to another place. I'm using D:\MongoDB in this blog.

Choose Install MongoDB as a Service

If you choose to install MongoDB as a service, the installation will create a Windows service so MongoDB will run in the background. Most of the time, you should choose this option.

Choose Data Directory and Log Directory

By default, MongoDB will store logs under the MongoDB installation location. For example, I set D:\MongoDB, so my logs will be at D:\MongoDB\log. The data directory will be under D:\MongoDB\data. However, you can change these to another location if you want.

That should be all you need to install the MongoDB server. If you just need the MongoDB server to run, you are done with the setup. For example, if you're installing MongoDB on a production server.

Install MongoDB Tools

Starting from MongoDB 4.4, the mongodump, mongoexport, mongoimport, and some other commands are not included as part of the MongoDB server setup. You have to install them separately. Go here and download the files. After downloading, you should see a list of files such as mongoexport and mongoimport under the bin folder. You'll need these to run mongoexport or mongoimport commands later.

Install MongoDB Compass

MongoDB Compass is the GUI tool. You should install it on your local development environment so you can connect to the MongoDB server and run queries from the GUI. If you have already installed MongoDB, after opening MongoDB Compass, you can type the following local URL to connect to the local instance of MongoDB.

mongodb://localhost:27017

Create Database, Collection, and Record

After you install MongoDB Compass and connect to the localhost MongoDB instance, you should be able to easily create a new database, collection, and record.

You could also use the command line or the MONGOSH BETA command line window in the Compass UI.

Create Database

show dbs
use myNewDatabase

First, show dbs will list all the databases. The use myNewDatabase command will create a database, but you won't see it until you add a new collection.

Create Table and Record

db.myUser.insert({name: "john"});

This command will create a new table called myUser and insert a new record into the collection.

MongoDB Backup and Restore Data

At this point, you have the MongoDB server installed and can create databases and collections. Next, I'd like to try backing up and restoring data by using mongoexport and mongoimport. The mongoexport tool will export data from MongoDB as a JSON file, and then mongoimport can import the JSON file into MongoDB.

This works well in a small development environment when you want to export a set of data and then load it into another environment from a JSON file.

Use Mongoexport to Export Data to JSON

First, navigate to the location that has the mongoexport executable file, then run the following command.

mongoexport --collection=myUser --db=myDB --out=myUser.json

This command will look up the myDB database's myUser collection, then output the entire result into a myUser.json file.

Use Mongoimport to Import Data to the Existing MongoDB Server

The following command will use myUser.json and then override the myUser collection, using upsert mode so any existing matching records will be replaced.

mongoimport --collection=myUser --db=myDB --file=myUser.json --mode=upsert

Conclusion

That's how you can set up MongoDB on Windows as a development environment, create simple records, and back up and restore data. MongoDB documentation is much better compared to a few years ago, and you can find very detailed documentation about each step. Also, tools like Compass are very helpful.