search

Home  >  Q&A  >  body text

MongoDB 中如何查看被修改的行数

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

迷茫迷茫2767 days ago702

reply all(2)I'll reply

  • 阿神

    阿神2017-04-22 09:01:51

    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

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-22 09:01:51

    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

    reply
    0
  • Cancelreply