Clear db with mongoid3



我喜欢在每次运行单元测试并清除mongo db之前执行设置,我该如何使用mongoid?

我在谷歌搜索时找到了一些有关它的链接,但似乎没有任何用。

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

在牛角群中,此讨论(删除牛角3中的所有集合)似乎是相关的。有两种方法purge!truncate!。吹扫会丢弃收藏,这也意味着索引。截断仅将文档放在每个集合中,这意味着您保留索引,但它比清除慢。

您可能想查看数据库cleaner gem,该宝石在规格中抽象清洁数据库

如果您使用的是导轨,则可以运行rake db:mongoid:purge以放下除系统集合以外的所有集合。

或运行 rake db:mongoid:drop以从当前轨道删除数据库。

最新更新