Class: Mongo::Operation::Write::Bulk::Insert::LegacyResult

Inherits:
Result
  • Object
show all
Includes:
LegacyMergable
Defined in:
lib/mongo/operation/write/bulk/insert/result.rb

Overview

Defines custom behaviour of results when inserting. For server versions < 2.5.5 (that don't use write commands).

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 included from LegacyMergable

#aggregate_write_concern_errors, #aggregate_write_errors

Methods inherited from Result

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

Constructor Details

#initialize(replies, ids) ⇒ LegacyResult

Initialize a new result.

Examples:

Instantiate the result.

Result.new(replies, inserted_ids)

Parameters:

  • replies (Protocol::Reply)

    The wire protocol replies.

  • ids (Array<Object>)

    The ids of the inserted documents.

Since:

  • 2.0.0



92
93
94
95
# File 'lib/mongo/operation/write/bulk/insert/result.rb', line 92

def initialize(replies, ids)
  @replies = replies.is_a?(Protocol::Reply) ? [ replies ] : replies
  @inserted_ids = ids
end

Instance Attribute Details

#inserted_idsObject (readonly)

Get the ids of the inserted documents.

Since:

  • 2.0.0



81
82
83
# File 'lib/mongo/operation/write/bulk/insert/result.rb', line 81

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



121
122
123
# File 'lib/mongo/operation/write/bulk/insert/result.rb', line 121

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



105
106
107
108
109
110
111
# File 'lib/mongo/operation/write/bulk/insert/result.rb', line 105

def n_inserted
  return 0 unless acknowledged?
  @replies.reduce(0) do |n, reply|
    n += 1 unless reply_write_errors?(reply)
    n
  end
end