简体   繁体   中英

Atomic removal of mongo collection using ruby driver

My current rails app is backed by mongoid/mongo. Between each test I want to clear the database. To this I run

::Mongoid.database.collections.select { |c| c.name !~ /^system/ }.each { |c| c.remove() }

Problem is the removal of the collection seems to run in the background. Sometimes the next test will start, insert a document and then have it cleared by the remove operation. Is there any way to make the collection removal blocking?

I understand there is an $atomic option, having looked at the source for mongo/collection I can't see any way to pass the option in. How can I make the collcetion removal blocking?

Mongoid when you call remove, simply passes what you send as arguments to the underlying mongo ruby driver. From API documentation http://api.mongodb.org/ruby/1.2.1/Mongo/Collection.html#remove-instance_method safe=>true blocks until it is done. So:

::Mongoid.database.collections.select { |c| c.name !~ /^system/ }.each { |c| c.remove(:safe => true) }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM