Clear mongodb in 3 lines of code using Mongoid

As always I am learning more and more about ruby and rails. Recently I choose to take a stab at using Mongodb as my database and move away from using more traditional DBs such as Sqlite and Postgres.

It has been a little strange not having to run rake tasks such a migrate to setup new database tables and such. If you find yourself needing to clear out all the data in the database and you are using the Mongoid gem such as I am in your rails project then you can run the rails console and do the following to clear everything out. I find this useful if you have been developing and need to clear out all your test data.

Mongoid.database.collections.each do |collection|
      unless =~ /^system./

Leave a comment

Your email address will not be published. Required fields are marked *