How to Setup MongoDB on Windows

2021/2/34 min read
bookmark this
Responsive image

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 store 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 following the installation wizard to install MongoDB. 

Choose Location

The default location is under windows' C drive, you can modify the location to other places. I'm using the D:\MongoDB on this blog.

Choose Install MongoDB as Service

If you choose to install MongoDB as a service, the installation will create a windows service so the MongoDB will be running as background. I think most of the time you should choose.

Choose Data Directory & Log Directory

By default, MongoDB will store logs under the MongoDB installation location. For example, I set D:\MongoDB, so my logs will be D:\MongoDB\log. Same as the data should be under D:\MongoDB\data, however, you can change to another location if you want.

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

Install MongoDB Tools

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

Install MongoDB Compass

MongoDB Compass is the GUI tool, you should install on your local development environment so you can connect to the MongoDB server and run the query from GUI. If you installed your MongoDB already, after open the 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 new database, collection, and record. 

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

Create Database

show dbs
use myNewDatabase

 First show dbs will list all the databases, the use myNewDtabase 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 myUser and insert a new record into the collection.

MongoDB Backup and Restore Data

At this point, you can MongoDB server installed and able to create a database and collection, next I'd like to try to backup and restore data by using mongoexport and mongoimport. The mongoexport will export the data from MongoDB as JSON file, then use mongoimport can import the JSON file into the MongoDB. 

This will work in a small development environment, as you want to export a list of data then dump it into another environment from this JSON file. 

Use Mongoexport to export data to JSON

First, cd 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 whole result into myUser.json file. 

Use Mongoimport to import data to the existing MongoDB Server

Following command will use myUser.json and then override the myUser collection, used mode as upsert so any existing same record 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, and how to create simple records along with backup and restore data. MongoDB documentation is much better to compare to a few years back, you can find very detailed documentation about each step you need. Also, the tool like Compass is very helpful.