Find and delete everything in mongo / mongoid

Is there a way in mongoid to find and "read" all the documents in the collection, and also delete them in one atomic query?

So far I have used:

Model.collection.find().to_json
Model.delete_all

What can be easily broken by adding more data to the collection between these two instructions.

+5
source share
2 answers

You can, for example, to rename the underlying collection, for example tmp_cf20c448f824763454ada2c4b2434178. Then all the new entries will go to the new, automatically created collection with the old name, and you can read all the documents without haste and worry, and then simply abandon this temp collection.

ruby ​​(10gen mongo mongoid 2.x moped mongoid 3.x).

+5

. :

Model.collection.drop

(, ) .

+9

All Articles