Chen Kinnrot
Chen Kinnrot

Reputation: 21015

Clear db with mongoid3

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.

Upvotes: 4

Views: 2987

Answers (4)

Santhosh
Santhosh

Reputation: 29174

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

Upvotes: 5

Tonez
Tonez

Reputation: 319

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.

Upvotes: 1

Alon Burg
Alon Burg

Reputation: 2550

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

Upvotes: 2

rubish
rubish

Reputation: 10907

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.

Upvotes: 3

Related Questions