How to Setup MongoDB on Mac

2020/07/113 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 macOS, create a simple database, table, and collection, and how to backup and restore data by using mongoexport and mongoimport.

Install MongoDB Server

Installing MongoDB on Mac is a little different compared to Windows. You'll need Homebrew to install MongoDB. This blog assumes Homebrew is already installed on your Mac.

Download Homebrew Formula for MongoDB

brew tap mongodb/brew

Install MongoDB on macOS

brew install mongodb-community@4.4

Install MongoDB Tools

Starting from MongoDB 4.4, using Homebrew will also install mongodump, mongoexport, mongoimport, and some other command-line tools.

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.

To install MongoDB Compass on macOS, go to the official site here to download the dmg file and install it on your macOS.

To connect to the localhost MongoDB instance, just enter the following URL in the MongoDB Compass UI.

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 macOS, create databases and collections, and back up and restore data using mongoexport and mongoimport. MongoDB Compass is a very helpful GUI tool for managing your local MongoDB instance.