Installing MongoDB on Mac OS X Mavericks for Node.js development

; Date: Fri Jan 17 2014

Tags: MongoDB »»»» MacOS X setup »»»» Mongoose »»»» Node.js

How do you upload files to a server to deploy application or website code?  FTP?  rsync?  While it's easy enough to call a command line tool like rsync from a Node.js script, what if you're using a Windows computer that doesn't have those command line tools.  When I use Windows it's like stepping back into the dark ages where directory listings looked like we were making fire by rubbing sticks together.  Okay, there does appear to be an rsync for Windows but I had no confidence in it.  Also, I did not want to have a dependency on something like Cygwin.

This is part three of a series of blog posts exploring the set-up of MAMP-like development environment for Node.js on Mac OS X.  Earlier we looked at setting up Node.js on a Mac OS X machine, as well as using forever to keep server processes running.  In this installment we'll look at setting up a MongoDB instance.

(www.mongodb.org) MongoDB is an extremely popular NoSQL database that's document centric and offers a lot of flexibility.  The Node.js community has developed several Mongo drivers for use with Node.  My book (see links in sidebar) includes a section demonstrating using the Mongoose ORM library.  Both the MongoDB and Mongoose projects are sponsored by (10gen.com) 10gen.

The first question to ask yourself is, why set up a local MongoDB instance at all?  There are several companies offering free or low cost MongoDB database services.  You could just do your software development work against one of those providers, and skip the headache of running your own server.

That's a great point and it's worth considering (www.mongohosting.com) http://www.mongohq.com for your MongoDB needs.  But, it's not that simple.  For example in my book I'd listed two other companies, neither of whom now offer MongoDB hosting services.  You'd be putting your development environment at the risk of 3rd parties who might quit their business.  Also, do you trust your data to a 3rd party?

In any case, assuming you've decided to set up a local MongoDB instance, how do you go about it?

One way is to go here (www.mongodb.org) http://www.mongodb.org/downloads and get the Mac OS X download.  They also have installation instructions for use either with the download, or a source install using Homebrew.

What I've done instead, on my computer, is install it with MacPorts.

Why install using a package manager rather than the downloadable binary?  It's so that the package manager system will automagically update the software as new releases come out.

With MacPorts it's real simple to do:

$ sudo port install mongodb
Password:
--->  Computing dependencies for mongodb
--->  Dependencies to be installed: libpcap scons snappy
... lots of output as it builds other stuff
--->  Building mongodb
--->  Staging mongodb into destroot
--->  Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting mongodb with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo port load mongodb
###########################################################
>--->  Installing mongodb @2.4.8_1
>--->  Activating mongodb @2.4.8_1

MongoDB is installed at this point, but not running.  As they note on the installation instructions (link above) you can simply run mongod with a local data directory, without having to mess around with getting Mac OS X to autostart the mongod process on system restart.  But, fortunately, MacPorts makes it easy to autostart MongoDB.

$ sudo port load mongodb
Password:
$ ps -eaf | grep mongo
    0 35355     1   0 12:24PM ??         0:00.01 /opt/local/bin/daemondo --label=mongodb --start-cmd sudo -u _mongo /opt/local/bin/mongod --dbpath /opt/local/var/db/mongodb --logpath /opt/local/var/log/mongodb/mongodb.log --logappend ; --pid=exec
    0 35356 35355   0 12:24PM ??         0:00.01 sudo -u _mongo /opt/local/bin/mongod --dbpath /opt/local/var/db/mongodb --logpath /opt/local/var/log/mongodb/mongodb.log --logappend
  506 35357 35356   0 12:24PM ??         0:00.11 /opt/local/bin/mongod --dbpath /opt/local/var/db/mongodb --logpath /opt/local/var/log/mongodb/mongodb.log --logappend
  501 35364   485   0 12:24PM ttys001    0:00.00 grep mongo

Now, MongoDB is running, and will auto restart when the computer starts.

You can play with the mongo install like so:

mainmini:t david$ mongo
MongoDB shell version: 2.4.8
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
 http://docs.mongodb.org/
Questions? Try the support group
 http://groups.google.com/group/mongodb-user
> 
> 
> help
 db.help()                    help on db methods
 db.mycoll.help()             help on collection methods
 sh.help()                    sharding helpers
 rs.help()                    replica set helpers
 help admin                   administrative help
 help connect                 connecting to a db help
 help keys                    key shortcuts
 help misc                    misc things to know
 help mr                      mapreduce
 
 show dbs                     show database names
 show collections             show collections in current database
 show users                   show users in current database
 show profile                 show most recent system.profile entries with time >= 1ms
 show logs                    show the accessible logger names
 show log [name]              prints out the last segment of log in memory, 'global' is default
 use                 set current database
 db.foo.find()                list objects in collection foo
 db.foo.find( { a : 1 } )     list objects in foo where a == 1
 it                           result of the last line evaluated; use to further iterate
 DBQuery.shellBatchSize = x   set default number of items to display on shell
 exit                         quit the mongo shell

The (docs.mongodb.org) Getting Started notes are also useful for verifying the install went well.

Over on the Mongoose website they have (mongoosejs.com) a quick start guide.

There are plenty of other MongoDB tools available in the npm registry :- (npmjs.org) https://npmjs.org/search?q=mongodb

About the Author(s)

(davidherron.com) David Herron : David Herron is a writer and software engineer focusing on the wise use of technology. He is especially interested in clean energy technologies like solar power, wind power, and electric cars. David worked for nearly 30 years in Silicon Valley on software ranging from electronic mail systems, to video streaming, to the Java programming language, and has published several books on Node.js programming and electric vehicles.

Books by David Herron

(Sponsored)