Method: Mongo::Collection#drop

Defined in:
lib/mongo/collection.rb

#drop(opts = {}) ⇒ Result

Note:

An error returned if the collection doesn’t exist is suppressed.

Drop the collection. Will also drop all indexes associated with the collection, as well as associated queryable encryption collections.

Examples:

Drop the collection.

collection.drop

Parameters:

  • opts (Hash) (defaults to: {})

    The options for the drop operation.

  • options (Hash)

    a customizable set of options

Options Hash (opts):

  • :session (Session)

    The session to use for the operation.

  • :write_concern (Hash)

    The write concern options.

  • :encrypted_fields (Hash | nil)

    Encrypted fields hash that was provided to create collection helper.

Returns:

  • (Result)

    The result of the command.

Since:

  • 2.0.0



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/mongo/collection.rb', line 459

def drop(opts = {})
  client.with_session(opts) do |session|
    context = Operation::Context.new(
      client: client,
      session: session,
      operation_timeouts: operation_timeouts(opts)
    )
    temp_write_concern = write_concern
    write_concern = if opts[:write_concern]
                      WriteConcern.get(opts[:write_concern])
                    else
                      temp_write_concern
                    end
    operation = Operation::Drop.new({
      selector: { :drop => name },
      db_name: database.name,
      write_concern: write_concern,
      session: session,
    })
    tracer.trace_operation(operation, context, op_name: 'dropCollection') do
      maybe_drop_emm_collections(opts[:encrypted_fields], client, session) do
        do_drop(operation, session, context)
      end
    end
  end
end