Count of rows affected in a multiple row mongodb update

3.6k Views Asked by At

Using the mongo shell what needs to added to an update/delete query to also return the number of rows that were affected by the query.

1

There are 1 best solutions below

2
On BEST ANSWER

I don't think there is a way to get that number directly from the call, but you can call the following right afterwards:

db.runCommand({getLastError: 1})

This will return a json object. the "n" key in that object will tell you the number of documents affected:

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