I’ve been playing with mongodb for a little while now and here some basic issues that I faced and some setup help.
Setting up mongo is really straight forward in OS X. You may either download it from here or use homebrew to install it. If your using brew
brew install mongodb
should do it.
Skip over to the bin folder and launch it with
You’ll now see that the console is available for access. It can be launched by
and you should see a irb like shell.
Using MongoDB with Mysql while continuing to use Mysql as the primary datastore.
I have been looking into Mongo purely because I was told its highly performant but I still need my primary datastore to be mysql because I see the need for transactions in the future. I decided to go with mongoid (was recommended because its maintained and is unlikely to vanish soon) which only require you to add the gems to the Gemfile.
gem "mongoid", "~> 2.3" gem "bson_ext", "~> 1.4" # Run bundle install
If you decide to use Mongoid with MongoDB as your primary datastore you would have to follow these steps completely. However, since I needed to still retain my mysql configuration I only ran this step
rails g mongoid:config
This should generate a mongoid.yml file similar to your database.yml file in the config folder. A very well written blog post on how to setup users on mongodb and maintain it with mongoid is provided here and I strongly recommend reading it.
Running the generator now makes mongo your default datastore and using the generator would build models using mogoid and not ActiveRecord. To ensure that active record remains your primary orm add the following config to your application.rb
config.generators do |gen| gen.orm :active_record end
Now you have ActiveRecord as your primary ORM and you may generate models with mongoid when you need it.
Errors when installing MongoDb on Ubuntu
As before you can find the tar ball at the Mongodb downloads page but on installing and running mongo, I encountered this error.
exception in initAndListen std::exception: dbpath (/data/db/) does not exist, terminating dbexit: shutdown: going to close listening sockets... shutdown: going to flush oplog... shutdown: going to close sockets... shutdown: waiting for fs preallocator... shutdown: closing all files... closeAllFiles() finished dbexit: really exiting now
The error says that it can’t find the /data/db folder. So just create with the right user permissions and you should be good to go