[SERVER-13865] Shell returns incorrect WriteResult for compatibility-mode upsert with non-OID equality pred on _id field Created: 07/May/14  Updated: 11/Jul/16  Resolved: 23/May/14

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 2.6.1, 2.7.0
Fix Version/s: 2.6.2, 2.7.1

Type: Bug Priority: Major - P3
Reporter: J Rassi Assignee: Greg Studer
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Related
related to SERVER-1351 GLE upserted _id field should always ... Closed
is related to DRIVERS-151 Handle Bulk API edge case for pre-2.6... Closed
Tested
Operating System: ALL
Backport Completed:
Participants:

 Description   
Issue Status as of Jun 9, 2014

ISSUE SUMMARY
When using a 2.6 shell against a 2.4 server, or during the upgrade period where a 2.6 mongos talks to a 2.4 mongod server, the shell returns an incorrect WriteResult for upsert operations where the query predicate includes an equality match on _id with a non-ObjectID type. For example:

db.foo.update({_id:0},{$set:{a:1}},{upsert:true})

"When using a 2.6 shell against a 2.4 server, or during the upgrade period where a 2.6 mongos talks to a 2.4 mongod server..."
Note that non-ObjectID upsert _ids were not reported in v2.4, the issue is the incorrect upsert statistics.

Resolution Details:

The WriteResult returned is:

WriteResult({ "nMatched" : 1, "nUpserted" : 0 })

The correct WriteResult should be one of the following:

  
WriteResult({ "nMatched" : 0, "nUpserted" : 1 })
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "_id" : <value> })

USER IMPACT
The upsert statistics in WriteResult messages returned by the shell are incorrect.

WORKAROUNDS
getLastError should report the correct upsert statistics.

AFFECTED VERSIONS
MongoDB 2.6.0 and 2.6.1 are affected by this issue.

FIX VERSION
The fix is included in the 2.6.2 production release.

RESOLUTION DETAILS
Do not rely on the presence of the upserted field in the getLastError response to populate write results.

Original description

In compatibility mode, the shell returns an incorrect WriteResult for upserts where the query predicate includes an equality match on _id with a non-OID type.

The WriteResult returned is:

WriteResult({ "nMatched" : 1, "nUpserted" : 0 })

The correct WriteResult should be one of the following:

WriteResult({ "nMatched" : 0, "nUpserted" : 1 })
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "_id" : <value> })

Reproduce with:

MongoDB shell version: 2.6.1
connecting to: test
> db.version()
2.4.10
> db.foo.remove({})
Cannot use commands write mode, degrading to compatability mode
WriteResult({ "nRemoved" : 0 })
> db.foo.update({_id:ObjectId("ffffffffffffffffffffffff")},{$set:{a:1}},{upsert:true})
// OID type, correct result
WriteResult({
	"nMatched" : 0,
	"nUpserted" : 1,
	"_id" : ObjectId("ffffffffffffffffffffffff")
})
> db.foo.update({_id:0},{$set:{a:1}},{upsert:true})
// non-OID type, incorrect result
WriteResult({ "nMatched" : 1, "nUpserted" : 0 })



 Comments   
Comment by Githook User [ 01/Jun/14 ]

Author:

{u'username': u'gregstuder', u'name': u'Greg Studer', u'email': u'greg@10gen.com'}

Message: SERVER-13865 handle edge case for v2.4 when upserted _id not returned

(cherry picked from commit 090ea9a5ad1ed52e40b5a66df5ee1eab283845cb)
Branch: v2.6
https://github.com/mongodb/mongo/commit/6dc02ec1d08825bb5ecd9b76d786d0d86bf37885

Comment by Githook User [ 23/May/14 ]

Author:

{u'username': u'gregstuder', u'name': u'Greg Studer', u'email': u'greg@10gen.com'}

Message: SERVER-13865 disable multiversion test until backport
Branch: master
https://github.com/mongodb/mongo/commit/b74536cec6d91061f28f7432b98e6983332d4c60

Comment by Githook User [ 23/May/14 ]

Author:

{u'username': u'gregstuder', u'name': u'Greg Studer', u'email': u'greg@10gen.com'}

Message: SERVER-13865 handle edge case for v2.4 when upserted _id not returned
Branch: master
https://github.com/mongodb/mongo/commit/090ea9a5ad1ed52e40b5a66df5ee1eab283845cb

Comment by Greg Studer [ 16/May/14 ]

Driver changes/audit is probably needed as well, every driver does upconversion of GLE results.

Generated at Thu Feb 08 03:33:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.