MongoDB 中如何查看被修改的行数
迷茫
迷茫 2017-04-22 08:59:51
0
2
735

在 Mysql 中可用通过 affect_rows 来查看本次操作数据库中受影响的行数,但是在文本型数据库中怎么获取这些信息呢?Or 别的调试方式?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
阿神
db.runCommand({getLastError: 1})

In the output getLastError.n 参数就是受影响的记录。Mongo Manual is defined like this:

n reports the number of documents updated or removed, if the preceding operation was an update or remove operation.

Example:
There are two following records in one collecton

{ "_id" : ObjectId("533e5cfa8d6728aef1f00111"), "sex" : "male" }
{ "_id" : ObjectId("533e5d088d6728aef1f00112"), "sex" : "female" }
First

operaterun 一个 update

db.people.update({ "sex" : "male" }, { "sex" : "unknown"})
Operation

againrun getLassError

db.runCommand({getLastError: 1})
The results are as follows:

{
    "updatedExisting" : true,
    "n" : 1,
    "connectionId" : 1332,
    "err" : null,
    "ok" : 1
}

is 1. update 操作影响了 1 个记录,所以 n Then
operaterun 一个 remove

db.people.remove()
The results are as follows:

{
    "n" : 2,
    "connectionId" : 1332,
    "err" : null,
    "ok" : 1
}

Appears after operation. remove 操作影响了 2 个记录,所以 n 为 2。此时 "updatedExisting" : true 未在结果中出现,因为该信息只在 update

大家讲道理

In the json returned by the

update statement, the value with key n is the number of modified rows.
Print it out and see for yourself

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template