Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mongo/bulk_write/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def upserted_count
#
# @since 2.1.0
def upserted_ids
@results[UPSERTED_IDS]
@results[UPSERTED_IDS] || []
end

# Validates the bulk write result.
Expand Down
4 changes: 2 additions & 2 deletions lib/mongo/bulk_write/result_combiner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def result

def combine_counts!(result)
Result::FIELDS.each do |field|
if result.respond_to?(field)
results.merge!(field => (results[field] || 0) + result.send(field))
if result.respond_to?(field) && value = result.send(field)
results.merge!(field => (results[field] || 0) + value)
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/mongo/operation/write/bulk/delete/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ module Aggregatable
def n_removed
return 0 unless acknowledged?
@replies.reduce(0) do |n, reply|
n += reply.documents.first[Result::N]
if reply.documents.first[Result::N]
n += reply.documents.first[Result::N]
else
n
end
end
end
end
Expand Down
45 changes: 38 additions & 7 deletions lib/mongo/operation/write/bulk/update/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def n_upserted
return 0 unless acknowledged?
@replies.reduce(0) do |n, reply|
if upsert?(reply)
n += 1
n += reply.documents.first[UPSERTED].size
else
n += 0
n
end
end
end
Expand All @@ -65,14 +65,22 @@ def n_matched
return 0 unless acknowledged?
@replies.reduce(0) do |n, reply|
if upsert?(reply)
n += 0
reply.documents.first[N] - n_upserted
else
n += reply.documents.first[N]
if reply.documents.first[N]
n += reply.documents.first[N]
else
n
end
end
end
end

# 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
Expand All @@ -83,7 +91,11 @@ def n_matched
def n_modified
return 0 unless acknowledged?
@replies.reduce(0) do |n, reply|
n += reply.documents.first[MODIFIED] || 0
if n && reply.documents.first[MODIFIED]
n += reply.documents.first[MODIFIED]
else
nil
end
end
end

Expand Down Expand Up @@ -155,12 +167,31 @@ def n_matched
end
end
end
alias :n_modified :n_matched

# Gets the number of documents modified.
#
# @example Get the modified count.
# result.n_modified
#
# @return [ Integer ] The number of documents modified.
#
# @since 2.2.3
def n_modified
return 0 unless acknowledged?
@replies.reduce(0) do |n, reply|
if upsert?(reply)
n
else
updated_existing?(reply) ? n += reply.documents.first[N] : n
end
end
end

private

def upsert?(reply)
!updated_existing?(reply) && reply.documents.first[N] == 1
reply.documents.first[BulkWrite::Result::UPSERTED] ||
(!updated_existing?(reply) && reply.documents.first[N] == 1)
end

def updated_existing?(reply)
Expand Down
Loading