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

Inherits:
Result
  • Object
show all
Includes:
Aggregatable
Defined in:
lib/mongo/operation/insert/bulk_result.rb

Overview

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

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

#replies

Instance Method Summary collapse

Methods inherited from Result

#acknowledged?, #cluster_time, #cursor_id, #documents, #each, #inspect, #labels, #multiple?, #namespace, #ok?, #operation_time, #reply, #returned_count, #successful?, #validate!, #written_count

Constructor Details

#initialize(replies, 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.

  • ids (Array<Object>)

    The ids of the inserted documents.

Since:

  • 2.0.0



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongo/operation/insert/bulk_result.rb', line 39

def initialize(replies, ids)
  @replies = [*replies] if replies
  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)

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.

Get the ids of the inserted documents.

Since:

  • 2.0.0



28
29
30
# File 'lib/mongo/operation/insert/bulk_result.rb', line 28

def inserted_ids
  @inserted_ids
end

Instance Method Details

#inserted_idObject

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.

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



94
95
96
# File 'lib/mongo/operation/insert/bulk_result.rb', line 94

def inserted_id
  inserted_ids.first
end

#n_insertedInteger

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.

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



82
83
84
# File 'lib/mongo/operation/insert/bulk_result.rb', line 82

def n_inserted
  written_count
end