Class: Mongo::Operation::Insert::BulkResult
- 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.
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
-
#inserted_ids ⇒ Object
readonly
Get the ids of the inserted documents.
Attributes inherited from Result
#connection, #connection_description, #connection_global_id, #context, #replies
Instance Method Summary collapse
-
#initialize(replies, connection_description, connection_global_id, ids) ⇒ BulkResult
constructor
private
Initialize a new result.
-
#inserted_id ⇒ Object
Gets the id of the document inserted.
-
#n_inserted ⇒ Integer
Gets the number of documents inserted.
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.
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_ids ⇒ Object (readonly)
Get the ids of the inserted documents.
33 34 35 |
# File 'lib/mongo/operation/insert/bulk_result.rb', line 33 def inserted_ids @inserted_ids end |
Instance Method Details
#inserted_id ⇒ Object
Gets the id of the document inserted.
110 111 112 |
# File 'lib/mongo/operation/insert/bulk_result.rb', line 110 def inserted_id inserted_ids.first end |
#n_inserted ⇒ Integer
Gets the number of documents inserted.
97 98 99 |
# File 'lib/mongo/operation/insert/bulk_result.rb', line 97 def n_inserted written_count end |