Search code examples
mongodbmongodb-querymongodb-update

Count of rows affected in a multiple row mongodb update


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.


Solution

  • 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
    }