mongodb/mongoid · error · Mongoid::Errors::CreateCollectionFailure

Cannot create collection

Error message

Cannot create collection %{collection_name} with options %{collection_options}. The following error was raised: %{error}.

What it means

create_collection attempted to create the collection with its configured options and the server returned an error (invalid options, name conflicts, permissions). CreateCollectionFailure wraps the driver error with the collection name and options.

Solutions

  1. Fix the reported collection_options (e.g. invalid validators or collation)
  2. Verify database permissions and server-side errors, then retry creation
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at lib/mongoid/collection_configurable.rb:45 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/e571e8c1b340be00. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/collection_configurable.rb:45

        if /^system\./.match?(collection_name)
          # We do not do anything with system collections.
          return
        end

        collection.drop if force
        if coll_options = collection.database.list_collections(filter: { name: collection_name.to_s }).first
          raise Errors::DropCollectionFailure.new(collection_name) if force

          logger.debug(
            "MONGOID: Collection '#{collection_name}' already exists " +
            "in database '#{database_name}' with options '#{coll_options}'."
          )

        else
          begin
            collection.database[collection_name, storage_options.fetch(:collection_options, {})].create
          rescue Mongo::Error::OperationFailure => e
            raise Errors::CreateCollectionFailure.new(
              collection_name,
              storage_options[:collection_options],
              e
            )
          end
        end
      end
    end
  end
end

View on GitHub (pinned to 0da0e23d71)