Is there a function in mongo to return the number of documents that were updated in an update statement?
I have tried to use count()
but apparently the update statement only returns true or false so i think I'm getting the count of a string.
Thanks
Use the getLastError
command to get information about the result of your operation.
I don't know the ruby driver, but most drivers automatically do this in 'safe mode'. In safe mode, each write will examine the result of getLastError
to make sure the write was successful. The update operation should return an object that looks like the JSON object further down, and it includes the number of updated documents (n
). You can fine-tune the safe mode settings, but be warned that the default mode is "fire and forget", so safe mode is a good idea for many use cases.
In the shell,
> db.customers.update({}, {$set : {"Test" : "13232"}}, true, true);
> db.runCommand( "getlasterror" )
{
"updatedExisting" : true,
"n" : 3,
"connectionId" : 18,
"err" : null,
"ok" : 1
}
Here, I updated n = 3
documents. Note that by default, update operations in mongodb only apply to the first matched document. In the shell, the fourth parameter is used to indicate we want to update multiple documents.