-
Type: Task
-
Resolution: Done
-
Priority: Blocker - P1
-
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.