Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-962

Upgrading from 1.8 to 2.x: "E11000 duplicate key error index"

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Blocker - P1 Blocker - P1
    • 12_01_17
    • Affects Version/s: 2.0.6
    • Component/s: Public API
    • None
    • Environment:
      MongoDB 2.4, Ruby 2.2, OS X Yosemite

      I'm in the process of upgrading from 'mongo' gem 1.8 to 2.0.

      In my 1.8 setup, I had this code that was working fine:

      # works fine, no matter how many times you call it
      coll.update(
            {'_id' => 5,
             'p' => {"$ne" => pricevat}, :d => {"$ne" => dt}},
            {'$set' => {"p" => pricevat, :d => dt},
             '$push' => {"h" => {:d => dt, :p => pricevat }}}, {:upsert => true})
      

      In the process of upgrading, I transformed this query to what I think that would be the equivalent, compatible with v2.0.6:

             coll.find({'_id' => 5},
               'p' => {"$ne" => pricevat}, :d => {"$ne" => dt})
             .update_one({'$set' => {"p" => pricevat, :d => dt},
             '$push' => {"h" => {:d => dt, :p => pricevat }}}, {:upsert => true})
      

      This code runs the 1st time (creates the document) and the subsequent times throws the following error:

      Mongo::Error::OperationFailure: E11000 duplicate key error index: price_history.products.$_id_  dup key: { : 5 } (11000)
      

      It seems that `upsert` is not taken into account and the operation tries to create the document instead of updating it.

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            agis Agis
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: