Mongoid, MongoMapper, etc.

Solano CI sets up an isolated Mongo DB database for each parallel test worker. Note that multiple workers may connect to the same mongod server, but with different database names.

MongoDB configuration information is exported to the environment via one of of the following variables:

  • MONGODB_URL
  • MONGOHQ_URL
  • MONGOLAB_URI
  • TDDIUM_MONGOID_PORT - tcp port number on localhost
  • TDDIUM_MONGOID_DB - MongoDB database name for this worker

The mongo configuration is also written to two standard locations: config/mongoid.yml and config/mongo.yml. Both files have the same content.

The test section of a typical mongoid.yml is shown below; your mongo.yml will be similarly replaced with a generated version.

# config/mongoid.yml
test: &test
  host: <%= ENV['TDDIUM_MONGOID_HOST'] %>
  port: <%= ENV['TDDIUM_MONGOID_PORT'] %>
  database: <%= ENV['TDDIUM_MONGOID_DB'] %>

If you are using Ruby, recent versions of Mongoid automatically load config/mongoid.yml and ERB interpolate it. If the gem you are using to connect to MongoDB does not, you will need to edit your mongo initializer(s) to ERB interpolate mongo.yml. For example, with MongoMapper:

# config/initializers/mongo_mapper.rb
template = ERB.new(File.read(Rails.root.join('config', 'mongo.yml'))).result
mongo_config = YAML.load(template)
MongoMapper.setup(mongo_config, Rails.env)

Common Setup Issues

If you are using Ruby and see an error like this when using the mongoid_spacial gem:

Mongo::OperationFailure: Database command 'count' failed:
   (errmsg: 'exception: can't find special index: 2d for:
{ _id: ObjectId('50131870e4b0c802a414a493'), location:
  { $near: [ 37.776193, -122.419775 ], $maxDistance: 0.144 } }';
  code: '13038'; ok: '0.0')

You may need to run the db:mongoid:create_indexes task from your mongo hook. More information is available in the mongoid spacial trouble shooting documentation.