[Awesome Ruby Gem] Use database_cleaner-mongoid gem to clear MongoDB database for Mongoid

Database Cleaner Adapter for Mongoid

Clean your Mongoid databases with Database Cleaner.

See https://github.com/DatabaseCleaner/database_cleaner for more information.

Installation

You can install it as a gem:

1
$ gem install database_cleaner-mongoid

or add it into a Gemfile (Bundler):

1
2
3
4
5
6
7
# Gemfile

group :test do
# DatabaseCleaner/database_cleaner-mongoid
# https://github.com/DatabaseCleaner/database_cleaner-mongoid
gem 'database_cleaner-mongoid'
end

Then, run bundle install.

1
$ bundle install

Usages

Supported Strategies

The mongoid adapter only has one strategy: the deletion strategy.

Strategy configuration options

:only and :except may take a list of collection names:

1
2
3
4
5
# Only delete the "users" collection.
DatabaseCleaner[:mongoid].strategy = [:deletion, only: ["users"]]

# Delete all collections except the "users" collection.
DatabaseCleaner[:mongoid].strategy = [:deletion, except: ["users"]]

Adapter configuration options

#db defaults to the default Mongoid database, but can be specified manually in a few ways:

1
2
3
4
5
6
7
8
9
10
# Redis URI string:
DatabaseCleaner[:mongoid].db = :logs

# Back to default:
DatabaseCleaner[:mongoid].db = :default

# Multiple Mongoid databases can be specified:
DatabaseCleaner[:mongoid, db: :default]
DatabaseCleaner[:mongoid, db: :shard_1]
DatabaseCleaner[:mongoid, db: :shard_2]

References

[1] DatabaseCleaner/database_cleaner-mongoid - https://github.com/DatabaseCleaner/database_cleaner-mongoid

[2] database_cleaner-mongoid | RubyGems.org | your community gem host - https://rubygems.org/gems/database_cleaner-mongoid/

[3] DatabaseCleaner/database_cleaner: Strategies for cleaning databases in Ruby. Can be used to ensure a clean state for testing. - https://github.com/DatabaseCleaner/database_cleaner