How to install MongoDB locally to your Mac


Intermediate

MongoDB is a NoSQL database, used mostly together with Node.js. Here’s instructions how to install it locally on your Mac.

You need to have homebrew installed on your mac to run these commands. Start by opening a terminal and run following commands on system shell.

brew update
brew install mongodb

Then you need to create a directory where mongoDB can write data. Go to your system root and type following command to  terminal. This will make a default directory for mongo to use.

mkdir -p /data/db

Then go to your finder and make sure user account running mongodb has read and write permissions for the directory. Usually it doesn’t. After this run mongodb by typing this to terminal.

mongod

To start using mongodb open a new terminal window, locate where your mongodb is installed and go there. By default in Mac it’s /usr/local/var/mongodb. Then run this to open a mongo shell.

mongo

Now you have mongo shell and you can type commands to mongo like these

Create and use database

use myDBName

Create a collection

db.createcollection('myCollection') 

Insert stuff into your collection

db.myCollection.insert({firstName: 'Juha', lastName: 'Stenroos'}) 

Find stuff from your collection. Adding pretty makes it more readable.

db.myCollection.find().pretty()

 

More commands to mongoDB shell can be found here.