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
9 changes: 6 additions & 3 deletions lib/mongo/bulk_write/result_combiner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ def combine_counts!(result)

def combine_ids!(result)
if result.respond_to?(Result::INSERTED_IDS)
results.merge!(Result::INSERTED_IDS => result.inserted_ids)
results[Result::INSERTED_IDS] = (results[Result::INSERTED_IDS] || []) +
result.inserted_ids
end
if result.respond_to?(Result::UPSERTED)
results.merge!(Result::UPSERTED_IDS => result.upserted.map{ |doc| doc['_id'] })
results[Result::UPSERTED_IDS] = (results[Result::UPSERTED_IDS] || []) +
result.upserted.map{ |doc| doc['_id'] }
end
end

Expand All @@ -109,7 +111,8 @@ def combine_write_errors!(result)

def combine_write_concern_errors!(result)
if write_concern_errors = result.aggregate_write_concern_errors(count)
results.merge!(Error::WRITE_CONCERN_ERRORS => write_concern_errors)
results[Error::WRITE_CONCERN_ERRORS] = (results[Error::WRITE_CONCERN_ERRORS] || []) +
write_concern_errors
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/mongo/bulk_write_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,20 @@
expect(result.modified_count).to eq(1)
expect(authorized_collection.find(_id: 0).first[:name]).to eq('test')
end

context 'when the number of updates exceeds the max batch size', if: write_command_enabled? do

let(:requests) do
1001.times.collect do |i|
{ update_one: { filter: { a: i }, update: { "$set" => { a: i, b: 3 }}, upsert: true }}
end
end

it 'updates the documents and reports the correct number of upserted ids' do
expect(result.upserted_ids.size).to eq(1001)
expect(authorized_collection.find(b: 3).count).to eq(1001)
end
end
end

context 'when providing a single update many' do
Expand Down Expand Up @@ -302,6 +316,10 @@
it 'inserts the documents' do
expect(result.inserted_count).to eq(1001)
end

it 'combines the inserted ids' do
expect(result.inserted_ids.size).to eq(1001)
end
end
end

Expand Down
18 changes: 17 additions & 1 deletion spec/support/shared/bulk_write.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@

let(:operations) do
[{ update_one: { find: { a: 1 },
update: { '$st' => { field: 'blah' } },
update: { '$set' => { field: 'blah' } },
upsert: false }
}]
end
Expand Down Expand Up @@ -522,6 +522,22 @@
bulk.execute
expect(authorized_collection.find.projection(_id: 0).to_a).to eq(expected)
end

context 'when the number of updates exceeds the max batch size' do

let(:operations) do
1001.times.collect do |i|
{ update_one: { find: { _id: i },
update: { '$set' => { _id: i } },
upsert: true }
}
end

it 'reports the full number of upserted ids' do
expect(bulk.execute.upserted_ids.size).to eq(1001)
end
end
end
end
end
end
Expand Down