I'm updating multiple elements in mongodb. Is it possible to return the number of affected objects?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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.