How to return number of updated objects in mongodb?

北战南征 提交于 2019-12-09 11:20:51

问题


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.



来源:https://stackoverflow.com/questions/4596846/how-to-return-number-of-updated-objects-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!