如何在mongodb中返回更新对象的数量?

时间:2011-01-04 18:07:34

标签: mongodb mongodb-query

我正在更新mongodb中的多个元素。是否可以返回受影响对象的数量?

1 个答案:

答案 0 :(得分:16)

使用getLastError。 n 键将包含更新文档的数量

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

请注意,这会运行命令“getLastError”,它返回更新命令完成后的行数。

数据库命令是listed here