The Little Things #5: initialize a local mongo db

--

I usually test my MongoDB projects using a cloud instance of the database — such as ClearDB through Azure. However, if you have to work offline at some point, it can be important to have a local testing ground; not to mention a place to test your initialization scripts.

I didn’t find ONE prescriptive place when I was getting started with MongoDB to help me do this quickly, so, in this post I’ll summarize how I set that up on my OSX environment using bash commands.

Step 1: Install MongoDB

Interestingly, I had already installed MongoDB on my local machine, which I discovered by trying to install it using homebrew:
brew install mongodb

For the life of me couldn’t remember where I installed it, and so I went ahead and unlinked it per the brew instruction, and reinstalled:
brew unlink mongodb
brew install mongodb

Step 2: Create the Data Directory

The next step is to create the data directory. I opted for the default path to avoid having to configure MongoDB to find another:

 sudo mkdir -p /data/db

Step 3: Run MongoDB

Next, run MongoDB and ensure no errors initializing:


./usr/local/Cellar/mongodb/2.6.4/bin/mongod

Step 4: Connect to the local MongoDB

Open another terminal window to interact with the running instance of MongoDB.

 mongo

This connects you to the default database, “test”. You can also find out which databases are installed by default with this command:
show dbs

Step 5: Create a DB (optional)

If you prefer work against a named database instance for your project, create one with this command:


use ProjectDBName

Yup. That creates it. Done.

Type db to see what db you are currently connected to, now it should be the new one.

Step 6: Create DB scripts (optional)

To seed the database, you can create a script file that operates against this db object. For example:

db.shippingList.insert({name:"FEDEX"}); db.shippingList.insert({name:"UPS"}); db.shippingList.insert({name:"DHL"}); db.shippingList.insert({name:"Other"});

Here’s an example with multiple columns:

db.serviceSpeedList.insert({  name:"DHL", service:"DHL Ground"}); db.serviceSpeedList.insert({  name:"DHL", service:"DHL Second Day Service"}); db.serviceSpeedList.insert({  name:"DHL", service:"DHL Express"}); db.serviceSpeedList.insert({  name:"DHL", service:"DHL International Express"}); db.serviceSpeedList.insert({  name:"DHL", service:"DHL Next Afternoon"});

Any json combination, really:

db.someStuff.insert({  orderId:1,  info:"Here's some info at the top level.",  children: ["child1","child2","child3"],  status:"Pending" });

Step 6: Execute Scripts (optional)

If you want to seed the database, run the scripts you created in the previous step by loading the file the text is located in, from within the MongoDB session you already established. The assumption here is the db is pointing at the right database so note that the scripts use db.* syntax.

load(“[yourpath]/seedscript.js”); 

HINT: if you get an error, uhm, it didn’t run. If it returns true, it did :)

If you run show collections after this, you should see your collections initialized by the script are now present!

Originally published on August 20, 2014.

--

--

Cofounder / CIO Solliance; Cloud / Security Architect; Microsoft Regional Director and Azure MVP; author Learning WCF, Developing Microsoft Azure Solutions