Skip to content
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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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

@jamis
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 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
@tom-selander
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants