Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

RUBY-3355 - Create when key '_id' (as string) is nil results in error #2808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
DrissTM wants to merge 1 commit into mongodb:master
base: master
Choose a base branch
Loading
from DrissTM:fix-nil-id-causing-multiple-id-fields-in-one-document

Conversation

@DrissTM
Copy link

@DrissTM DrissTM commented Nov 16, 2023

Hi!

While investigating an issue in an application I'm working on I discovered that trying to insert a document containing '_id' => nil will raise the following exception:

.../lib/mongo/operation/result.rb:364:in `raise_operation_failure': [2]: can't have multiple _id fields in one document (on 127.0.0.1:27017, legacy retry, attempt 1) (Mongo::Error::OperationFailure)

Here's a reproduction script:

require 'bundler/inline'
gemfile do
 source 'https://rubygems.org'
 gem 'mongo'
end
client = Mongo::Client.new(['127.0.0.1:27017'], database: 'PoC_DTM') # Assuming you have a mongo server running on port 27017
db = client.database
collection = db[:fake_collection]
collection.insert_one({ field: 'test', _id: nil }) # => OK
collection.insert_one({ field: 'test', '_id' => nil }) # => KO

I was able to track down the issue in the lib/mongo/operation/shared/idable.rb file:

...
 def id(doc)
 doc.respond_to?(:id) ? doc.id : (doc['_id'] || doc[:_id])
 end
 def has_id?(doc)
 !!id(doc)
 end
 def ensure_ids(documents)
 @ids = []
 documents.collect do |doc|
 doc_with_id = has_id?(doc) ? doc : doc.merge(_id: id_generator.generate)
 @ids << id(doc_with_id)
 doc_with_id
 end
 end
...

When '_id' is equal to nil has_id? returns false which causes ensure_ids to generate an id that it stores in :_id but without removing the '_id' key which results in the document having both '_id' and :_id, hence the exception mentioned above.


Let me know if I should create an issue in JIRA first

Copy link
Contributor

jamis commented Nov 16, 2023

Thank you! I've added a Jira ticket here (https://jira.mongodb.org/browse/RUBY-3355) to track this. Thank you for finding this, and for the pull request!

DrissTM reacted with thumbs up emoji

@DrissTM DrissTM changed the title (削除) Fix: Delete "_id" key from document if equal to nil to avoid having two different _id keys in the document (削除ここまで) (追記) RUBY-3355 - Create when key '_id' (as string) is nil results in error (追記ここまで) Nov 16, 2023
Copy link

Hi @DrissTM thanks for filing this PR. We have tagged this for consideration next quarter and have backlogged the associated Jira ticket until then. Please follow/watch https://jira.mongodb.org/browse/RUBY-3355 for updates.

DrissTM reacted with thumbs up emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /