How to view the number of modified rows in MongoDB

  mongodb, question

In Mysql, affect_rows can be used to view the number of rows affected in the database of this operation, but how to obtain this information in a text database? Or other debugging methods?

db.runCommand({getLastError: 1})

In outputgetLastError.nParameters are affected records.Mongo ManualIt is defined as follows:

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

For example:
In onecollectonThere are two following records in

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

FirstrunOneupdateOperation

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

Againrun getLassErrorOperation

db.runCommand({getLastError: 1})

The results are as follows:

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

updateThe operation affected 1 record, sonIs 1.
AgainrunOneremoveOperation

db.people.remove()

The results are as follows:

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

removeThe operation affected 2 records, sonTwo. At this time"updatedExisting" : trueDid not appear in the results because this information is only available inupdateAppears after the operation.