Module: Mongoid::Persistable::Deletable::ClassMethods

Defined in:
lib/mongoid/persistable/deletable.rb

Overview

Since:

  • 4.0.0

Instance Method Summary collapse

Instance Method Details

#delete_all(conditions = nil) ⇒ Integer

Delete all documents given the supplied conditions. If no conditions are passed, the entire collection will be dropped for performance benefits. Does not fire any callbacks.

Examples:

Delete matching documents from the collection.

Person.delete_all({ :title => "Sir" })

Delete all documents from the collection.

Person.delete_all

Parameters:

  • conditions (Hash) (defaults to: nil)

    Optional conditions to delete by.

Returns:

  • (Integer)

    The number of documents deleted.

Since:

  • 1.0.0



138
139
140
141
142
143
144
145
# File 'lib/mongoid/persistable/deletable.rb', line 138

def delete_all(conditions = nil)
  selector = conditions || {}
  selector.merge!(_type: name) if hereditary?
  coll = collection
  deleted = coll.find(selector).count
  coll.find(selector).delete_many
  deleted
end