123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Operation::Insert::BulkResult

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Mongo::Operation::Result, Forwardable
Instance Chain:
self, Aggregatable, ::Mongo::Operation::Result, Enumerable
Inherits: Mongo::Operation::Result
Defined in: lib/mongo/operation/insert/bulk_result.rb

Overview

Defines custom behavior of results for an insert when sent as part of a bulk write.

Since:

  • 2.0.0

Constant Summary

::Mongo::Operation::Result - Inherited

CURSOR, CURSOR_ID, FIRST_BATCH, N, NAMESPACE, NEXT_BATCH, OK, RESULT

Class Method Summary

::Mongo::Operation::Result - Inherited

.new

Initialize a new result.

Instance Attribute Summary

::Mongo::Operation::Result - Inherited

#acknowledged?

Is the result acknowledged?

#connection, #connection_description, #connection_global_id, #context,
#ok?

Check the first document's ok field.

#replies,
#successful?

If the result was a command then determine if it was considered a success.

#write_concern_error?

Whether the operation failed with a write concern error.

#query_failure?

Instance Method Summary

::Mongo::Operation::Result - Inherited

#cluster_time

Get the cluster time reported in the server response.

#cursor_id

Get the cursor id if the response is acknowledged.

#documents

Get the documents in the result.

#each

Iterate over the documents in the replies.

#error

The exception instance (of Error::OperationFailure::Family) that would be raised during processing of this result.

#inspect

Get the pretty formatted inspection of the result.

#labels

Gets the set of error labels associated with the result.

#n
#namespace

Get the namespace of the cursor.

#operation_time

Get the operation time reported in the server response.

#reply

Get the reply from the result.

#returned_count

Get the number of documents returned by the server in this batch.

#snapshot_timestamp, #topology_version,
#validate!

Validate the result by checking for any errors.

#written_count

Get the number of documents written by the server.

#first_document, #operation_failure_class, #parser,
#raise_operation_failure

Raises a Mongo::OperationFailure exception corresponding to the error information in this result.

Constructor Details

.new(replies, connection_description, connection_global_id, ids) ⇒ BulkResult

Initialize a new result.

Examples:

Instantiate the result.

Result.new(replies, inserted_ids)

Parameters:

  • replies (Array<Protocol::Message> | nil)

    The wire protocol replies, if any.

  • connection_description (Server::Description)

    ::Mongo::Server description of the server that performed the operation that this result is for.

  • connection_global_id (Integer)

    Global id of the connection on which the operation that this result is for was performed.

  • ids (Array<Object>)

    The ids of the inserted documents.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/operation/insert/bulk_result.rb', line 49

def initialize(replies, connection_description, connection_global_id, ids)
  super(replies, connection_description, connection_global_id)
  if replies && replies.first && (doc = replies.first.documents.first)
    if (errors = doc['writeErrors'])
      # some documents were potentially inserted
      bad_indices = {}
      errors.map do |error|
        bad_indices[error['index']] = true
      end
      @inserted_ids = []
      ids.each_with_index do |id, index|
        @inserted_ids << id if bad_indices[index].nil?
      end
    # I don't know if acknowledged? check here is necessary,
    # as best as I can tell it doesn't hurt
    elsif acknowledged? && successful?
      # We have a reply and the reply is successful and the
      # reply has no writeErrors - everything got inserted
      @inserted_ids = ids
    else
      # We have a reply and the reply is not successful and
      # it has no writeErrors - nothing got inserted.
      # If something got inserted the reply will be not successful
      # but will have writeErrors
      @inserted_ids = []
    end
  else
    # I don't think we should ever get here but who knows,
    # make this behave as old drivers did
    @inserted_ids = ids
  end
end

Instance Attribute Details

#inserted_ids (readonly)

Get the ids of the inserted documents.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/operation/insert/bulk_result.rb', line 31

attr_reader :inserted_ids

Instance Method Details

#inserted_idObject

Gets the id of the document inserted.

Examples:

Get id of the document inserted.

result.inserted_id

Returns:

  • (Object)

    The id of the document inserted.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/operation/insert/bulk_result.rb', line 104

def inserted_id
  inserted_ids.first
end

#n_insertedInteger

Gets the number of documents inserted.

Examples:

Get the number of documents inserted.

result.n_inserted

Returns:

  • (Integer)

    The number of documents inserted.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/operation/insert/bulk_result.rb', line 91

def n_inserted
  written_count
end