Definition
WriteResult()A wrapper that contains the result status of
mongoshwrite methods.Note
db.collection.insert()anddb.collection.update()are deprecated.The results returned by the replacement methods have a different format. For output similar to
WriteResult(), consider usingdb.collection.bulkWrite().
Compatibility
This method is available in deployments hosted in the following environments:
MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise: The subscription-based, self-managed version of MongoDB
MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB
Properties
The WriteResult() has the following properties:
WriteResult.nInsertedThe number of documents inserted, excluding
upserteddocuments. SeeWriteResult.nUpsertedfor the number of documents inserted through an upsert.
WriteResult.nMatchedThe number of documents selected for update. If the update operation results in no change to the document, e.g.
$setexpression updates the value to the current value,nMatchedcan be greater thannModified.
WriteResult.nModifiedThe number of existing documents updated. If the update/replacement operation results in no change to the document, such as setting the value of the field to its current value,
nModifiedcan be less thannMatched.
WriteResult.nUpsertedThe number of documents inserted by an upsert.
WriteResult._idThe
_idof the document inserted by anupsert. Returned only if anupsertresults in an insert.
WriteResult.writeErrorA document that contains information regarding any error, excluding write concern errors, encountered during the write operation.
WriteResult.writeConcernErrorDocument describing errors that relate to the write concern.
Changed in version 5.0.30: When
WriteResult()is received frommongos, write concern errors are always reported, even when one or more write errors occur. In previous releases, the occurrence of write errors could cause theWriteResult()to not report write concern errors.Each error document contains the following fields:
WriteResult.writeConcernError.codeAn integer value identifying the cause of the write concern error.
WriteResult.writeConcernError.errInfo.writeConcernThe write concern object used for the corresponding operation. For information on write concern object fields, see Write Concern Specification.
The write concern object may also contain the following field, indicating the source of the write concern:
WriteResult.writeConcernError.errInfo.writeConcern.provenanceA string value indicating where the write concern originated (known as write concern
provenance). The following table shows the possible values for this field and their significance:ProvenanceDescriptionclientSuppliedThe write concern was specified in the application.
customDefaultThe write concern originated from a custom defined default value. See
setDefaultRWConcern.getLastErrorDefaultsThe write concern originated from the replica set's
settings.getLastErrorDefaultsfield.implicitDefaultThe write concern originated from the server in absence of all other write concern specifications.