2012-11-04 27 views
4

我喜歡在每次運行單元測試並清除我的mongo db之前執行一個設置,我該如何使用mongoid?清除db與mongoid3

我發現了一些關於它的鏈接,而谷歌搜索,但似乎沒有工作。

回答

5

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 
1

這個discussion(刪除Mongoid 3中的所有集合)在mongoid組似乎是相關的。有兩種方法purge!truncate!。清除會丟棄集合,這也意味着索引。截斷僅刪除每個集合中的文檔,這意味着您保留了索引,但它比清除更慢。

2

你可能想看看database_cleaner寶石其抽象如果你使用Rails在清洗

規格
+0

謝謝,我用這種寶石。 –

0

數據庫,你可以運行rake db:mongoid:purge刪除所有收藏品,除了系統集合。

或者,運行rake db:mongoid:drop將數據庫從當前的Rails.env中刪除。

輸出的