Mihails Strasuns
Mihails Strasuns

Reputation: 3803

MongoDB shell: check if update succeeded

Similar to MongoDB update: how to check if an update succeeds or fails? but for default mongodb shell. db.collection.update() will execute silently in both cases: when query has found a document and when not. And getLastError is also null after both updates.

How can I find out that something was actually updated without re-querying collection?

I am using MongoDB version 2.0.4 on Ubuntu 12.04

Upvotes: 4

Views: 4645

Answers (1)

mjhm
mjhm

Reputation: 16695

The db.getLastErrorObj() is what you want to call to get the result of the update. It returns an object that looks like:

{
    "updatedExisting" : true,
    "n" : 2,
    "connectionId" : 35,
    "err" : null,
    "ok" : 1
}

n is the number of updated documents.

Upvotes: 6

Related Questions