class Mongo::Operation::Update::BulkResult

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

@since 2.0.0

Constants

MODIFIED

The number of modified docs field in the result.

@since 2.0.0

UPSERTED

The upserted docs field in the result.

@since 2.0.0

Public Instance Methods

n_matched() click to toggle source

Gets the number of documents matched.

@example Get the matched count.

result.n_matched

@return [ Integer ] The number of documents matched.

@since 2.0.0

# File lib/mongo/operation/update/bulk_result.rb, line 62
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_modified() click to toggle source

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.

@example Get the modified count.

result.n_modified

@return [ Integer ] The number of documents modified.

@since 2.0.0

# File lib/mongo/operation/update/bulk_result.rb, line 89
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_upserted() click to toggle source

Gets the number of documents upserted.

@example Get the upserted count.

result.n_upserted

@return [ Integer ] The number of documents upserted.

@since 2.0.0

# File lib/mongo/operation/update/bulk_result.rb, line 43
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
upserted() click to toggle source

Get the upserted documents.

@example Get upserted documents.

result.upserted

@return [ Array<BSON::Document> ] The upserted document info

@since 2.1.0

# File lib/mongo/operation/update/bulk_result.rb, line 108
def upserted
  return [] unless acknowledged?
  @replies.reduce([]) do |ids, reply|
    if upserted_ids = reply.documents.first[UPSERTED]
      ids += upserted_ids
    end
    ids
  end
end

Private Instance Methods

upsert?(reply) click to toggle source
# File lib/mongo/operation/update/bulk_result.rb, line 120
def upsert?(reply)
  upserted.any?
end