Class: Mongo::Operation::Write::Insert::Result

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

Overview

Defines custom behaviour of results for an insert.

According to the CRUD spec, reporting the inserted ids is optional. It can be added to this class later, if needed.

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?, #cursor_id, #documents, #each, #inspect, #multiple?, #namespace, #ok?, #reply, #returned_count, #successful?, #validate!, #written_count

Constructor Details

#initialize(replies, ids) ⇒ Result

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



42
43
44
45
# File 'lib/mongo/operation/write/insert/result.rb', line 42

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



31
32
33
# File 'lib/mongo/operation/write/insert/result.rb', line 31

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



55
56
57
# File 'lib/mongo/operation/write/insert/result.rb', line 55

def inserted_id
  inserted_ids.first
end