Getting Started with Mongo on Windows

April 26, 2010

Currently I’m writing a simple Rails app in an attempt to learn about 100 new things at once: Ruby on Rails, VIM, and of course Mongo DB.  There were a few steps to remember in order to get Mongo to work with my app, so I figured I’d document them here, in one spot, so that maybe they’d help someone out.

1. Install the Mongo gems

First, I installed the following gems:

  • mongo
  • mongo_mapper

MongoMapper is a great little wrapper around Mongo that makes your domain objects still feel like you’re using ActiveRecord objects.  The link gives a great little demo on how to get started using it, and it really is quite simple.

2. Update your environment file

In your Rails app, go to your environment.rb file, and uncomment the following line:

config.frameworks -= [:active_record, etc...]

Since we don’t need ActiveRecord anymore, it just seemed to make sense not to include it anymore. However I don’t believe this step is required.

Also in your environment.rb file, add the following lines to ensure the gems are used throughout the app:

config.gem ‘mongo’
config.gem ‘mongo_mapper’

3. Update Your Database Connection

Update your database.yml file to use the Mongo adapter.  It should look something like:

development:
  adapter: mongodb
  database: mydatabase

etc...

Next, create a file in your config/initializers directory, which will tell Mongo where to get the database connection info from.  In my case, I created a file named mongodb.rb, and it contains the following:

db_config = YAML::load(File.read(RAILS_ROOT + "/config/database.yml"))

if db_config[Rails.env] && db_config[Rails.env]['adapter'] == 'mongodb'
  mongo = db_config[Rails.env]
  MongoMapper.connection = Mongo::Connection.new(mongo['hostname'])
  MongoMapper.database = mongo['database']
end

The first line tells Mongo to look at the database.yml file to retrieve the appropriate connection properties (ex: hostname and database).

4. Install and run Mongo

You can download the Mongo install file here: http://www.mongodb.org/display/DOCS/Downloads

This will download a zip  file which you can unzip anywhere.  As an example, let’s say you unzipped your file to c:\mongodb.  You will also need a place to store the database (since it’s file system-based).  For myself, I created a directory labeled c:\mongodb\db.

Now you need to run Mongo.  This can be done by going to your command prompt and navigating to the directory you just unzipped to (ex: c:\mongodb\mongodb-win32-i386-1.4.1\bin) and execute the mongod.exe file, and supply the path to the db directory you created earlier, like so:

mongod.exe –dbpath c:\mongodb\db

Now Mongo is up and running, enjoy!

Advertisement

One Response to “Getting Started with Mongo on Windows”

  1. [...] Getting Started with Mongo on Windows (Erik Peterson) [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.