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

Inherits:
Result
  • Object
show all
Includes:
Aggregatable
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

Constants inherited from Result

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

Instance Attribute Summary collapse

Attributes inherited from Result

#connection, #connection_description, #connection_global_id, #context, #replies

Instance Method Summary collapse

Methods inherited from Result

#acknowledged?, #cluster_time, #cursor_id, #documents, #each, #error, #has_cursor_id?, #inspect, #labels, #namespace, #ok?, #operation_time, #reply, #returned_count, #snapshot_timestamp, #successful?, #topology_version, #validate!, #write_concern_error?, #written_count

Constructor Details

#initialize(replies, connection_description, connection_global_id, ids) ⇒ BulkResult

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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)

    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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mongo/operation/insert/bulk_result.rb', line 51

def initialize(replies, connection_description, connection_global_id, ids)
  @replies = [*replies] if replies
  @connection_description = connection_description
  @connection_global_id = 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|
        if bad_indices[index].nil?
          @inserted_ids << id
        end
      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_idsObject (readonly)

Get the ids of the inserted documents.

Since:

  • 2.0.0



33
34
35
# File 'lib/mongo/operation/insert/bulk_result.rb', line 33

def inserted_ids
  @inserted_ids
end

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



110
111
112
# File 'lib/mongo/operation/insert/bulk_result.rb', line 110

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



97
98
99
# File 'lib/mongo/operation/insert/bulk_result.rb', line 97

def n_inserted
  written_count
end