We use MongoDB as our primary store and have built a healthy amount of automation around various database instances and operational environments. For example, we backup databases to S3 using
mongodump, mirror data between instances with
mongorestore and often need to open a MongoDB shell with
mongo to examine data at the lowest level.
Generating MongoDB command-lines is tedious and error-prone. Introducing a new gem called mongoid-shell to help with this. The library can generate command-lines for various MongoDB shell tools from your Mongoid configuration.
For example, connect to your production MongoDB instance from a
db:production:shell Rake task.
1 2 3 4 5 6 7 8
Commands can be created for the current default session or you can pass a session as an argument to a new command.
1 2 3 4 5 6
Commands accept parameters. Here's how to backup
1 2 3 4 5
The mongoid-shell gem currently supports
mongostat and various MongoDB configurations, including replica-sets.
Please note that we don't recommend you store passwords for production environments in your
mongoid.yml. At Artsy, we set all sensitive values directly on our Heroku instances with
heroku config:add and use heroku-commander to retrieve these settings in rake. We also have a bit of convention in our application name, such as "app-staging" and "app-production".
Here's a complete Rake task that dynamically fetches Heroku configuration and opens a MongoDB shell on a production or staging environment.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
rake db:staging:shell or
rake db:production:shell, which works as long as you have access to the Heroku app itself. A bonus feature is that the mongoid-shell gem will automatically connect to the primary node of a replica-set.
1 2 3 4 5