定义
WriteResult()A wrapper that contains the result status of
mongoshwrite methods.注意
db.collection.insert()和db.collection.update()已弃用。The results returned by the replacement methods have a different format. For output similar to
WriteResult(), consider usingdb.collection.bulkWrite().
兼容性
此方法可用于以下环境中托管的部署:
- MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
属性
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.nUpsertedupsert 插入的文档数。
WriteResult.writeConcernError描述与写关注(write concern)相关的错误的文档。
在版本 7.0.6 中进行了更改:
(also available in 6.0.14 and 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.WriteResult.writeConcernError.errInfo.writeConcern用于相应操作的写关注对象。有关写关注对象字段的信息,请参阅写关注规范。
写关注对象还可能包含以下字段,指示写关注的来源:
WriteResult.writeConcernError.errInfo.writeConcern.provenance一个表示写关注来源(称为写关注
provenance)的字符串值。下表显示该字段的可能值及其有效位数:来源说明clientSupplied应用程序中指定了写关注。
customDefault写入关注源自自定义的默认值。请参阅
setDefaultRWConcern。getLastErrorDefaults写关注源自副本集的
settings.getLastErrorDefaults字段。implicitDefault在没有所有其他写入关注规范的情况下,写入关注源自服务器。