OPTIONS
翻译或纠错本页面

写关注

Write concern describes the guarantee that MongoDB provides when reporting on the success of a write operation. The strength of the write concerns determine the level of guarantee. When inserts, updates and deletes have a weak write concern, write operations return quickly. In some failure cases, write operations issued with weak write concerns may not persist. With stronger write concerns, clients wait after sending a write operation for MongoDB to confirm the write operations.

MongoDB提供了不同等级的写关注以更好地满足不同应用的特定需求。客户端可以调整写关注, 以保证最重要的操作能够成功存留在一个完整的MongoDB部署中。对于其它不那么重要的操作,客户端可以调整写关注,以保证更快的性能而不是整个部署的一致性。

在 2.6 版更改: 一个新的 写入操作 协议将写关注及写入操作进行了整合。

如果你想了解更多关于写关注配置的细节,请查阅 Write Concern Reference

考量

默认的写关注

The mongo shell and the MongoDB drivers use 确认型(Acknowledged) as the default write concern.

请查阅 确认型(Acknowledged) 以了解更多信息,包括写关注什么时候变为默认。

读隔离

MongoDB允许客户端读取那些在将这些修改提交到硬盘之前被插入或者修改的文档,而不用考虑写关注等级或日志的配置。因此,应用也许会观察到两个类的行为:

  • 对于拥有多个并行读数器及写入器的系统而言,MongoDB 将会允许客户端在写入操作返回之前读取一个写入操作的结果。

  • 如果 mongod 在日志提交之前终止了,即使一个写入成功返回,在 mongod 重启之前,查询也将有可能读取不到相关的数据。

其它的数据库系统将这些隔离语义称为 未授权读取-允许脏读取 。 对于所有的插入和更新,MongoDB单独修改每一个文档:客户端永远不会看到中间状态的文档。对于多文档操作,MongoDB并不提供任何多文档的事物或隔离。

mongod 返回一个成功的 日志型写关注 时, 数据将完整提交到磁盘, 并且在 mongod 重启之后可以使用。

对于副本集而言,写入操作只有在一个写入副本并且提交到该集合主要成员的日志后才能持久化。 MongoDB定期将数据提交到日志,不用考虑日志型写关注: 使用 commitIntervalMs 来控制 mongod 提交到日志的时间间隔。

超时

客户端可以设置一个 超时 值作为 副本集确认型 写关注的一部分。如果写关注不满足特定的时间间隔,即使最后这个写关注最后回成功,这个操作仍然将会返回一个错误。

MongoDB 不会 ” 回滚 ” 或者撤销 wtimeout 间隔过期之前的修改。

写关注等级

MongoDB有如下几个级别的概念性写关注,从最弱到最强排列如下:

无确认型(Unacknowledged)

通过使用 无确认型(unacknowledged) 写关注,MongoDB 不用对写入操作的接收进行应答。 无未确认型(Unacknowledged)* 与 errors ignored 相似,但是驱动器尽可能地尝试接收和解决网络错误。 驱动器检测网络错误的能力视系统的网络配置而定。

Default Write Concern Change 中列出的版本之前,这是默认的写入关注。

Write operation to a ``mongod`` instance with write concern of ``unacknowledged``. The client does not wait for any acknowledgment.

Write operation to a mongod instance with write concern of unacknowledged. The client does not wait for any acknowledgment.

确认型(Acknowledged)

通过使用一个应答 确认型(Acknowledged) 写关注, mongod 保证了写入操作的应答。 确认型(Acknowledged) 允许客户端捕捉网络、重复键值以及其它错误。

Releases 列出的驱动器版本开始, MongoDB就默认使用 确认型(acknowledged) 写关注。

在 2.6 版更改: 目前, mongo 脚本的写入方法包含了 写关注 中的写入方法,并且在交互式运行或者脚本中都提供了默认的写关注。请查阅 Write Method Acknowledgements 了解更多细节。

Write operation to a ``mongod`` instance with write concern of ``acknowledged``. The client waits for acknowledgment of success or exception.

Write operation to a mongod instance with write concern of acknowledged. The client waits for acknowledgment of success or exception.

日志型

通过使用一个 日志型(journaled) 写关注, MongoDB只在将数据提交到 journal 之后才认可这个写入操作。这个写关注保证了MongoDB可以在一个关机或者供电中断之后恢复相关的数据。

为了使用这个写关注,您必须启用日志记录。

通过使用一个 日志型(journaled) 写关注,写入操作必须等到下一个日志提交才能够继续执行。为了减少这些操作的延迟,MongoDB增加了提交操作到日志的频率,请查阅 commitIntervalMs 以了解更多信息。

Write operation to a ``mongod`` instance with write concern of ``journaled``. The ``mongod`` sends acknowledgment after it commits the write operation to the journal.

Write operation to a mongod instance with write concern of journaled. The mongod sends acknowledgment after it commits the write operation to the journal.

注解

在一个副本集中要求 日志型(journaled) 写关注只需要将该写操作的日志提交到集合的 primary,而不用考虑 replica acknowledged 写关注的等级。

副本集确认型(Replica Acknowledged)

Replica sets present additional considerations with regards to write concern.. The default write concern only requires acknowledgement from the primary.

通过使用 副本集确认型(replica acknowledged) 写关注,您可以保证写入操作拓展到副本集中其它的成员。请查阅 副本集的写关注 以了解更多信息.

Write operation to a replica set with write concern level of ``w:2`` or write to the primary and at least one secondary.

Write operation to a replica set with write concern level of w:2 or write to the primary and at least one secondary.

注解

在一个副本集中要求 日志型(journaled) 写关注只需要将该写操作的日志提交到集合的 primary,而不用考虑 replica acknowledged 写关注的等级。