123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Operation::Update::BulkResult Private

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Mongo::Operation::Result, Forwardable
Instance Chain:
self, Aggregatable, ::Mongo::Operation::Result, Enumerable
Inherits: Mongo::Operation::Result
Defined in: lib/mongo/operation/update/bulk_result.rb

Overview

Defines custom behavior of results for an udpate when sent as part of a bulk write.

Since:

  • 2.0.0

Constant Summary

::Mongo::Operation::Result - Inherited

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

Class Method Summary

::Mongo::Operation::Result - Inherited

.new

Initialize a new result.

Instance Attribute Summary

::Mongo::Operation::Result - Inherited

#acknowledged?

Is the result acknowledged?

#connection, #connection_description, #connection_global_id, #context,
#has_cursor_id?

Whether the result contains cursor_id.

#ok?

Check the first document’s ok field.

#replies,
#successful?

If the result was a command then determine if it was considered a success.

#write_concern_error?

Whether the operation failed with a write concern error.

#query_failure?

Instance Method Summary

::Mongo::Operation::Result - Inherited

#cluster_time

Get the cluster time reported in the server response.

#cursor_id

Get the cursor id if the response is acknowledged.

#documents

Get the documents in the result.

#each

Iterate over the documents in the replies.

#error

The exception instance (of Error::OperationFailure::Family) that would be raised during processing of this result.

#inspect

Get the pretty formatted inspection of the result.

#labels

Gets the set of error labels associated with the result.

#n
#namespace

Get the namespace of the cursor.

#operation_time

Get the operation time reported in the server response.

#reply

Get the reply from the result.

#returned_count

Get the number of documents returned by the server in this batch.

#snapshot_timestamp, #topology_version,
#validate!

Validate the result by checking for any errors.

#written_count

Get the number of documents written by the server.

#aggregate_returned_count, #aggregate_written_count, #first_document, #operation_failure_class, #parser,
#raise_operation_failure

Raises a Mongo::OperationFailure exception corresponding to the error information in this result.

Instance Method Details

#n_matchedInteger

Gets the number of documents matched.

Examples:

Get the matched count.

result.n_matched

Returns:

  • (Integer)

    The number of documents matched.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/operation/update/bulk_result.rb', line 65

def n_matched
  return 0 unless acknowledged?
  @replies.reduce(0) do |n, reply|
    if upsert?(reply)
      reply.documents.first[N] - n_upserted
    else
      if reply.documents.first[N]
        n += reply.documents.first[N]
      else
        n
      end
    end
  end
end

#n_modifiedInteger

Gets the number of documents modified. Not that in a mixed sharded cluster a call to update could return nModified (>= 2.6) or not (<= 2.4). If any call does not return nModified we can’t report a valid final count so set the field to nil.

Examples:

Get the modified count.

result.n_modified

Returns:

  • (Integer)

    The number of documents modified.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/operation/update/bulk_result.rb', line 92

def n_modified
  return 0 unless acknowledged?
  @replies.reduce(0) do |n, reply|
    if n && reply.documents.first[MODIFIED]
      n += reply.documents.first[MODIFIED]
    else
      0
    end
  end
end

#n_upsertedInteger

Gets the number of documents upserted.

Examples:

Get the upserted count.

result.n_upserted

Returns:

  • (Integer)

    The number of documents upserted.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/operation/update/bulk_result.rb', line 46

def n_upserted
  return 0 unless acknowledged?
  @replies.reduce(0) do |n, reply|
    if upsert?(reply)
      n += reply.documents.first[UPSERTED].size
    else
      n
    end
  end
end

#upsert?(reply) ⇒ Boolean (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/operation/update/bulk_result.rb', line 123

def upsert?(reply)
  upserted.any?
end

#upsertedArray<BSON::Document>

Get the upserted documents.

Examples:

Get upserted documents.

result.upserted

Returns:

  • (Array<BSON::Document>)

    The upserted document info

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/operation/update/bulk_result.rb', line 111

def upserted
  return [] unless acknowledged?
  @replies.reduce([]) do |ids, reply|
    if upserted_ids = reply.documents.first[UPSERTED]
      ids += upserted_ids
    end
    ids
  end
end