db.runCommand({getLastError: 1}) 在输出中 getLastError.n 参数就是受影响的记录。Mongo Manual 是这样定义的: n reports the number of documents updated or removed, if the preceding operation was an update or remove operation. 举例说明: 在一个 collecton 中有两条如下的记录 { "_id" : ObjectId("533e5cfa8d6728aef1f00111"), "sex" : "male" } { "_id" : ObjectId("533e5d088d6728aef1f00112"), "sex" : "female" } 先 run 一个 update 操作 db.people.update({ "sex" : "male" }, { "sex" : "unknown"}) 再 run getLassError 操作 db.runCommand({getLastError: 1}) 结果如下: { "updatedExisting" : true, "n" : 1, "connectionId" : 1332, "err" : null, "ok" : 1 } update 操作影响了 1 个记录,所以 n 为 1。 再 run 一个 remove 操作 db.people.remove() 结果如下: { "n" : 2, "connectionId" : 1332, "err" : null, "ok" : 1 } remove 操作影响了 2 个记录,所以 n 为 2。此时 "updatedExisting" : true 未在结果中出现,因为该信息只在 update 操作后出现。
在输出中
getLastError.n参数就是受影响的记录。Mongo Manual是这样定义的:举例说明:
在一个
collecton中有两条如下的记录先
run一个update操作再
run getLassError操作结果如下:
update操作影响了 1 个记录,所以n为 1。再
run一个remove操作结果如下:
remove操作影响了 2 个记录,所以n为 2。此时"updatedExisting" : true未在结果中出现,因为该信息只在update操作后出现。