Docs HomeMongoDB Manual

Mongo.setWriteConcern()

On this page本页内容

Definition定义

Mongo.setWriteConcern()

Sets the write concern for the Mongo() connection object.设置Mongo()连接对象的写入关注

See the Write Concern for an introduction to write concerns in MongoDB.有关在MongoDB中编写关注的介绍,请参阅写入关注

Syntax语法

The command takes the following form:该命令采用以下形式:

db.getMongo().setWriteConcern( { w: <value>, j: <boolean>, wtimeout: <number> } )

The fields are:字段为:

Field字段Description描述
wThe number of mongod or mongod instances that must acknowledge a write. 必须确认写入的mongodmongod实例数。Possible values are: 可能的值为:
  • "majority". A majority of the target instances must acknowledge the write."majority"。大多数目标实例必须确认写入。
  • <number>. The specified number of target instances must acknowledge the write.<number>。指定数量的目标实例必须确认写入。
  • <custom write concern name>. A user defined write concern, the tagged instances must acknowledge the write.<custom write concern name>。对于用户定义的写入关注,标记的实例必须确认写入。
See write concern specification for details. 有关详细信息,请参阅写入关注规范
jA boolean value. 布尔值。j: true requests acknowledgment that the write operation has been written to the on-disk journal.请求确认写入操作已写入磁盘上的日志
wtimeoutThe number of milliseconds to wait for acknowledgement of the write concern. 等待确认写入关注的毫秒数。wtimeout is only applicable when w has a value greater than 1.只有当w的值大于1时,wtimeout才适用。

Example实例

In the following example:在以下示例中:

  • Two mongod or mongod instances must acknowledge writes.两个mongodmongod实例必须确认写入。
  • There is a 1 second timeout to wait for write acknowledgements.1秒的超时等待写入确认。
db.getMongo().setWriteConcern( { w: 2, wtimeout: 1000 } )
Tip

See also: 另请参阅: