Class: Mongo::Operation::Write::Bulk::Update::Result

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

Overview

Defines custom behaviour of results when updating.

Since:

  • 2.0.0

Constant Summary

MODIFIED =

The number of modified docs field in the result.

Since:

  • 2.0.0

'nModified'.freeze
UPSERTED =

The upserted docs field in the result.

Since:

  • 2.0.0

'upserted'.freeze

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

Attributes inherited from Result

#replies

Instance Method Summary collapse

Methods included from Mergable

#aggregate_write_concern_errors, #aggregate_write_errors

Methods inherited from Result

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

Constructor Details

This class inherits a constructor from Mongo::Operation::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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mongo/operation/write/bulk/update/result.rb', line 64

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



91
92
93
94
95
96
97
98
99
100
# File 'lib/mongo/operation/write/bulk/update/result.rb', line 91

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
      nil
    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



45
46
47
48
49
50
51
52
53
54
# File 'lib/mongo/operation/write/bulk/update/result.rb', line 45

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

#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



110
111
112
# File 'lib/mongo/operation/write/bulk/update/result.rb', line 110

def upserted
  reply.documents.first[UPSERTED] || []
end