1

Why does getLastError show that no docs were updated when it was?

> db.contacts.find()
{ "_id" : ObjectId("509b60e7c546b6dc73f57877"), "name" : "mike" }
> db.contacts.update({}, {name: 'peter'})
> db.runCommand({getLastError: 1})
{ "n" : 0, "connectionId" : 3188, "err" : null, "ok" : 1 }
> db.contacts.find()
{ "_id" : ObjectId("509b60e7c546b6dc73f57877"), "name" : "peter" }

getLastError is returning n: 0, even though a document was clearly updated. It's also missing the updatedExisting field. I'm running this remotely on a sample MongoHQ database.

Run against my local MongoDB instance, getLastError correctly returns this:

> db.runCommand({getLastError: 1})
{
    "updatedExisting" : true,
    "n" : 1,
    "connectionId" : 1,
    "err" : null,
    "ok" : 1
}
Mike M. Lin
  • 9,992
  • 12
  • 53
  • 62
  • 1
    This appears to happen for me when I use a 2.0 version of the Mongo shell with a 2.2 database. Can you try running it with the 2.2 or 2.2.1 version of shell? – MrKurt Nov 09 '12 at 00:42

1 Answers1

2

This might be a problem with connection re-use or it could be the shell behavior. The getLastError (GLE) call just returns the status of the last operation to happen on the connection that executes the GLE call.

However, when you are using the shell, it automatically calls GLE after every write operation, so you will usually get a null result because GLE has already been called. Try calling getPrevError() instead - see if that returns what you expected.

The MongoDB drivers take care of this by making sure that GLE is called before a connection is returned to the pool and that is returned as the result (or error) for an operation to avoid this kind of problem.

Adam Comerford
  • 21,336
  • 4
  • 65
  • 85
  • Thanks. I didn't realize there was a layer between my client and the MongoDB instance when using MongoHQ. And had I known, I wouldn't have guessed I could get different connections from the pool within the same shell session. I'll leave this question open for a day or two to see if I can validate that or get a more definite answer. – Mike M. Lin Nov 08 '12 at 18:39
  • 1
    There's not actually a layer between Mongo and your app with MongoHQ, so this probably isn't correct. – MrKurt Nov 09 '12 at 00:39
  • Misunderstanding on my part then - any way you can think of for the GLE to be hitting a different connection or returning for a different op? – Adam Comerford Nov 09 '12 at 01:34
  • had a chat with some people - alternative explanation suggested - thanks @MrKurt :) – Adam Comerford Nov 09 '12 at 01:44
  • Interesting. Your alternative explanation seems to be correct. Calling `getPrevError` does return what I need. Even more interesting is that the behavior is different against a local instance, where the call to `getLastError` returns what I expect. `getPrevError` works OK against the MongoHQ instance and the local one, however, the return property `nPrev` always returns 1 against the local instance, but increments each time I call it against the MongoHQ one. Wish I knew why. Thanks for the help, Everyone. – Mike M. Lin Nov 10 '12 at 09:47