4

I like to perform a set up before each time I run a unit test and clear my mongo db, how do I do it with mongoid?

I found some links about it while googling but nothing seemed to work.

Chen Kinnrot
  • 20,609
  • 17
  • 79
  • 141

4 Answers4

5

Output of rake -T

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
Santhosh
  • 28,097
  • 9
  • 82
  • 87
2

This discussion(Delete All Collections in Mongoid 3) in mongoid group seems relevant. There are two methods purge! and truncate!. Purge drops the collections, which means also the indexes. Truncate only drops the documents in each collection, which means you retain indexes, but it is slower than purge.

rubish
  • 10,887
  • 3
  • 43
  • 57
2

You might want to take a look at database_cleaner gem which abstracts cleaning databases in specs

Alon Burg
  • 2,500
  • 2
  • 27
  • 32
1

If you're using Rails, you can run rake db:mongoid:purge to drop all collections except the system collections.

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

Tonez
  • 319
  • 2
  • 5