[SERVER-5441] Mongo shell "getlasterror" does not return "updatedExisting" via mongos Created: 29/Mar/12  Updated: 11/Jul/16  Resolved: 04/Jun/12

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 2.0.2
Fix Version/s: 2.1.2

Type: Bug Priority: Minor - P4
Reporter: Nicholas Simmons Assignee: Eliot Horowitz (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
Operating System: ALL
Participants:

 Description   

I was testing the "Update if current" in mongo shell, using the documentation on the mongoDB website.

I tried to duplicate the little code snippet that was there, but the results I got back did not match. I was expecting to see the "updatedExisting" field when i called "getlasterror".

Here is exactly what i did from the mongoDB shell, along with the output.

mongos> db.t.insert(

{sku:'abc',qty:1}

)
mongos> db.t.find()

{ "_id" : ObjectId("4f746863fba63782ae1d93a1"), "sku" : "abc", "qty" : 1 }

mongos> db.t.update({sku:"abc",qty:{$gt:0}}, { $inc :

{ qty : -1 }

} )
mongos> db.$cmd.findOne(

{getlasterror:1}

)

{ "err" : null, "ok" : 1 }

mongos> db.t.find()

{ "_id" : ObjectId("4f746863fba63782ae1d93a1"), "sku" : "abc", "qty" : 0 }

mongos> db.t.update({sku:"abc",qty:{$gt:0}}, { $inc :

{ qty : -1 }

} )
mongos> db.$cmd.findOne(

{getlasterror:1}

)

{ "err" : null, "ok" : 1 }

mongos> db.t.find()

{ "_id" : ObjectId("4f746863fba63782ae1d93a1"), "sku" : "abc", "qty" : 0 }

 Comments   
Comment by auto [ 04/Jun/12 ]

Author:

{u'login': u'erh', u'name': u'Eliot Horowitz', u'email': u'eliot@10gen.com'}

Message: SERVER-5441 make replSetGetStatus with forShell not change getLastError in mongos
Branch: master
https://github.com/mongodb/mongo/commit/0adfc2f9274379e797d7c4d685abfac6047372c7

Comment by Nicholas Simmons [ 30/Mar/12 ]

Doing both operations on a single line did the trick.

Thanks!

Comment by Eliot Horowitz (Inactive) [ 30/Mar/12 ]

The problem is the shell is implicitly calling it changing state (which its not supposed to do).

try this on a single line

db.t.update({sku:"abc",qty:{$gt:0}}, { $inc : { qty : -1 } } );  db.$cmd.findOne({getlasterror:1})

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