Clear db with mongoid3

I like to do the setup before each unit test runs and clear my mongo db, how to do it with mongoid?

I found some links about this during the search, but nothing worked.

+4
source share
4 answers

rake -T output

 rake db:drop # Drops all the collections for the database for the current Rails.env .. rake db:mongoid:drop # Drops the database for the current Rails.env rake db:mongoid:purge # Drop all collections except the system collections .. rake db:purge # Drop all collections except the system collections 
+5
source

Maybe you should take a look at the database_cleaner gem, which abstracts the database cleanup in the specs

+2
source

This discussion (Delete all collections in Mongoid 3) in the mongoid group seems relevant. There are two methods of purge! and truncate! . Cleaning discards collections, which means indexes. Truncate only deletes documents in each collection, which means you keep indexes, but slower than cleanup.

+1
source

If you use Rails, you can run rake db:mongoid:purge to remove all collections except system collections.

Or run rake db:mongoid:drop to remove the database from the current Rails.env.

0
source

Source: https://habr.com/ru/post/1443917/


All Articles