Skip to content
Open
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
8 changes: 7 additions & 1 deletion lib/mongo/operation/shared/idable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ def has_id?(doc)
def ensure_ids(documents)
@ids = []
documents.collect do |doc|
doc_with_id = has_id?(doc) ? doc : doc.merge(_id: id_generator.generate)
doc_with_id = if has_id?(doc)
doc
else
doc.delete('_id')
doc.merge(_id: id_generator.generate)
end

@ids << id(doc_with_id)
doc_with_id
end
Expand Down
19 changes: 19 additions & 0 deletions spec/mongo/operation/insert_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@
expect(inserted_ids).to eq(collection_ids)
end
end

context 'when document contains a nil id' do

let(:documents) do
[{ 'field' => 'test', '_id' => nil }]
end

let(:inserted_ids) do
insert.execute(authorized_primary, context: context).inserted_ids
end

let(:collection_ids) do
authorized_collection.find(field: 'test').collect { |d| d['_id'] }
end

it 'adds an id to the documents' do
expect(inserted_ids).to eq(collection_ids)
end
end
end

describe '#execute' do
Expand Down