Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return number of updated objects in mongodb?

I'm updating multiple elements in mongodb. Is it possible to return the number of affected objects?

like image 552
potomok Avatar asked Jan 04 '11 18:01

potomok


1 Answers

Use getLastError. The n key will contain the number of updated documents

> db.count.update({x : 1}, {$inc : {x : 1}}, false, true)
> db.runCommand({getLastError : 1})
{
"err" : null,
"updatedExisting" : true,
"n" : 5,
"ok" : true
}

Note that this runs the command "getLastError" which returns the number of rows after the update command has completed.

Database commands are listed here.

like image 183
Javier Ferrero Avatar answered Oct 14 '22 22:10

Javier Ferrero