[SERVER-8312] return value "n" in db.getLastErrorCmd() have BUG Created: 24/Jan/13  Updated: 11/Jul/16  Resolved: 18/Feb/13

Status: Closed
Project: Core Server
Component/s: Querying, Write Ops
Affects Version/s: 2.2.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: HongYing Assignee: Ian Daniel
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

FreeBSD 7.3-RELEASE FreeBSD 7.3-RELEASE root@:/usr/src/sys/amd64/compile/GENERIC amd64
db version v2.2.0, pdfile version 4.5


Operating System: FreeBSD
Steps To Reproduce:

> db.bobo.save(

{a:1, b:2, c:3, d:4}

)
> db.bobo.find()

{ "_id" : ObjectId("5100b05af1c2c68819360d1a"), "a" : 1, "b" : 2, "c" : 3, "d" : 4 }

> db.bobo.update(

{a:1, b:2}

, {$unset:{c:1, d:1}})
> db.getLastErrorCmd()
{
"n" : 0, // <------- it expected to be `1`
"lastOp" :

{ "t" : 1358999706000, "i" : 1 }

,
"connectionId" : 131,
"err" : null,
"ok" : 1
}
> db.bobo.find(

{a:1, b:2}

)

{ "_id" : ObjectId("5100b05af1c2c68819360d1a"), "a" : 1, "b" : 2 }

> db.bobo.update(

{a:1}

, {$unset:{b:1}})
> db.getLastErrorCmd()
{
"updatedExisting" : true,
"n" : 1,
"lastOp" :

{ "t" : 1358999741000, "i" : 2 }

,
"connectionId" : 131,
"err" : null,
"ok" : 1
}

Participants:

 Description   

After updating and unsetting a field, we get result by db.getLastErrorCmd().
we found strange `n' value returned.
see below:



 Comments   
Comment by Ian Daniel [ 04/Feb/13 ]

Hi HongYing,

It looks like you might be running these commands interactively in the shell. db.getLastError() is unreliable in the shell. A carriage return in the shell results in GetLastError being run. This then produced unreliable results when you call it again.

There are several possible solutions:

1. Write up your commands into a file with a .js suffix, and pass it to mongo at the command line.

2. If running interactively, put the db.getLastErrorCmd() on the same line as the update (separated by a semi-colon). That is:

db.bobo.update({a:1, b:2}, {$unset:{c:1, d:1}}); db.getLastErrorCmd()

3. If you want to run it on a separate line, instead use the db.getPrevError() command.

4. Set verbose to true in the shell (set verbose true). This will then tell you how many objects were updated.

If none of these actually fixes the problem, are you sharding? I ask because in the just released 2.2.3 we fixed several bugs in which a mongos returns an incorrect GetLastError. You should try in 2.2.3 to see if this resolves the problem.

Kind regards,
Ian

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