Module: Mongoid::CollectionConfigurable::ClassMethods
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Extended In:
| |
Defined in: | lib/mongoid/collection_configurable.rb |
Instance Method Summary
-
#create_collection(force: false)
Create the collection for the called upon
::Mongoid
model.
Instance Method Details
#create_collection(force: false)
Create the collection for the called upon ::Mongoid
model.
This method does not re-create existing collections.
If the document includes store_in
macro with collection_options
key,
these are used when creating the collection.
# File 'lib/mongoid/collection_configurable.rb', line 24
def create_collection(force: false) if collection_name.empty? # This is most probably an anonymous class, we ignore them. return end if collection_name.match(/^system\./) # We do not do anything with system collections. return end if force collection.drop end if = collection.database.list_collections(filter: { name: collection_name.to_s }).first if force raise Errors::DropCollectionFailure.new(collection_name) else logger.debug( "MONGOID: Collection '#{collection_name}' already exists " + "in database '#{database_name}' with options '#{}'." ) end else begin collection.database[collection_name, .fetch(:, {})].create rescue Mongo::Error::OperationFailure => e raise Errors::CreateCollectionFailure.new( collection_name, [: ], e ) end end end