MongoDB update: how to check if an update succeeds or fails? MongoDB update: how to check if an update succeeds or fails? mongodb mongodb

MongoDB update: how to check if an update succeeds or fails?


the "n" field is the number of documents that were updated, and "updatedExisting" tells whether any document was updated at all. you should be able to check those fields to see whether an update succeeded or not.


How do you know what rows are affected with a MongoDB update?

As with all write operations, getLastError command in MongoDB can confirm the result of the update's write operation, described here:

http://docs.mongodb.org/manual/applications/update/

The getLastError command returns the error status of the last operation on the current connection. By default MongoDB does not provide a response to confirm the success or failure of a write operation, clients typically use getLastError in combination with write operations to ensure that the write succeeds.

http://docs.mongodb.org/manual/reference/command/getLastError/#getLastError