44Modify Execution of CRUD Operations
55===================================
66
7- .. default-domain:: mongodb
8-
97.. contents:: On this page
108 :local:
119 :backlinks: none
@@ -34,6 +32,8 @@ the following levels:
3432You should read this guide if you need to customize the consistency and
3533availability of the data in your replica sets.
3634
35+ .. _golang-writeconcern:
36+
3737Write Concern
3838-------------
3939
@@ -49,9 +49,9 @@ Options
4949
5050The {+driver-long+} provides the ``writeconcern`` package, which lets
5151you specify the write concern for a replica set. Set the write
52- concern using the ``SetWriteConcern()`` method with an ``Option ``
53- type. The ``Option `` type has the following methods to specify the
54- write concern:
52+ concern using the ``SetWriteConcern()`` method with a ``WriteConcern ``
53+ type. The ``WriteConcern `` type has the following methods to
54+ select common write concern specifications :
5555
5656.. list-table::
5757 :widths: 25 75
@@ -60,56 +60,110 @@ write concern:
6060 * - Method
6161 - Description
6262
63- * - ``J()``
64- - | The client requests acknowledgement that write operations are written to the
65- journal. For more information, see the
66- :rapid:`Write Concern specification </reference/write-concern/#j-option>`.
63+ * - ``Custom()``
64+ - | The client requests acknowledgement that write operations propagate to
65+ tagged members of a ``mongod`` instance. For more
66+ information, see the :rapid:`Write Concern specification
67+ </reference/write-concern/#mongodb-writeconcern-writeconcern.-custom-write-concern-name->`.
6768 |
68- | **Parameter**: ``bool ``
69+ | **Parameter**: ``string ``
6970
70- * - ``W ()``
71- - | The client requests acknowledgement that write operations propagate to the
72- specified number of ``mongod`` instances . For more information, see the
73- :rapid:`Write Concern specification </reference/write-concern/#mongodb-writeconcern-writeconcern.-number- >`.
71+ * - ``Journaled ()``
72+ - | The client requests acknowledgement that write operations are
73+ written to the on-disk journal . For more information, see the
74+ :rapid:`Write Concern specification </reference/write-concern/#j-option >`.
7475 |
75- | **Parameter**: ``int``
76+ | **Parameter**: none
7677
77- * - ``WMajority ()``
78+ * - ``Majority ()``
7879 - | The client requests acknowledgement that write operations propagate to the
79- majority of ``mongod`` instances . For more information, see the
80+ majority of data-bearing voting members . For more information, see the
8081 :rapid:`Write Concern specification
8182 </reference/write-concern/#mongodb-writeconcern-writeconcern.-majority->`.
8283 |
8384 | **Parameter**: none
8485
85- * - ``WTagSet ()``
86- - | The client requests acknowledgement that write operations propagate to the
87- specified ``mongod`` instance . For more
88- information, see the :rapid:`Write Concern specification
89- </reference/write-concern/#mongodb-writeconcern-writeconcern.-custom-write-concern-name ->`.
86+ * - ``Unacknowledged ()``
87+ - | The client requests requests no acknowledgment of write
88+ operations . For more information, see the
89+ :rapid:`Write Concern specification for w: 0
90+ </reference/write-concern/#mongodb-writeconcern-writeconcern.-number ->`.
9091 |
91- | **Parameter**: ``string``
92-
92+ | **Parameter**: none
93+
94+ * - ``W1()``
95+ - | The client requests acknowledgement that write operations have
96+ been written to memory on one node, such as the standalone mongod or
97+ the primary in a replica set. For more
98+ information, see the :rapid:`Write Concern specification for w: 1
99+ </reference/write-concern/#mongodb-writeconcern-writeconcern.-number->`.
100+ |
101+ | **Parameter**: none
102+
103+ If you require a more specialized write concern, you can define a custom
104+ ``WriteConcern`` struct literal. You can set the following fields in a
105+ ``WriteConcern`` struct:
106+
107+ .. list-table::
108+ :widths: 25 75
109+ :header-rows: 1
110+
111+ * - Field
112+ - Description
113+
114+ * - ``W``
115+ - | Specifies the number of ``mongod`` instances or tagged members
116+ that write operations must propagate to for acknowledgement. Common values include
117+ ``1``, ``0``, and ``"majority"``.
118+ |
119+ | **Type**: ``string`` or ``int``
120+
121+ * - ``Journal``
122+ - | Specifies if write operations need to be written to the on-disk
123+ journal for acknowledgement.
124+ |
125+ | **Type**: ``bool``
126+
127+ * - ``WTimeout``
128+ - | Specifies a time limit for the write concern. This setting is
129+ applicable only for ``W`` values greater than 1. Specifying this
130+ setting and specifying a :ref:`Timeout
131+ <golang-timeout-setting>` on the client at the same time
132+ results in undefined behavior. To learn more, see the
133+ :manual:`Write Concern specification for wtimeout </reference/write-concern/#wtimeout>`.
134+ |
135+ | **Type**: ``time.Duration``
136+
93137.. tip::
94138
95139 You can alternatively specify a write concern in your connection
96- string. See the :manual:`Server Manual entry on Write Concern Options
140+ string. See the :manual:`Server manual entry on Write Concern Options
97141 </reference/connection-string/#write-concern-options>` for more information.
98142
99143Example
100144~~~~~~~
101145
102- The following code shows how you can specify a write concern to request
103- acknowledgement from two replica set members. The code then creates a ``Client``
104- with this option.
146+ The following code shows how you can specify different write concerns
147+ at the client and collection level. The *client-level* write concern
148+ requests acknowledgement from two replica set members and sets journaling to
149+ ``false``. The *collection-level* write concern requests
150+ acknowledgement from the majority of replica set members.
105151
106152.. code-block:: go
107- :emphasize-lines: 2-3
153+ :emphasize-lines: 2-6,10-11
108154
109155 uri := "mongodb://<hostname>:<port>"
110- wc := writeconcern.W(2)
111- opts := options.Client().ApplyURI(uri).SetWriteConcern(writeconcern.New(wc))
112- client, err := mongo.Connect(context.TODO(), opts)
156+ cliWC := &writeconcern.WriteConcern{
157+ W: 2,
158+ Journal: false,
159+ }
160+ clOpts := options.Client().ApplyURI(uri).SetWriteConcern(cliWC)
161+ client, err := mongo.Connect(context.TODO(), clOpts)
162+ ...
163+
164+ collWC := writeconcern.Majority()
165+ collOpts := options.Collection().SetWriteConcern(collWC)
166+ coll := client.Database("myDB").Collection("myColl", collOpts)
113167
114168Read Concern
115169------------
@@ -210,30 +264,30 @@ type has the following methods to specify the read preference:
210264 * - ``Nearest()``
211265 - The client reads from a random eligible replica set member,
212266 primary or secondary, based on a specified latency threshold. For more information, see the
213- :rapid:`Read Preference Server Manual entry </core/read-preference/#mongodb-readmode-nearest>`.
267+ :rapid:`Read Preference Server manual entry </core/read-preference/#mongodb-readmode-nearest>`.
214268
215269 * - ``Primary()``
216270 - The client reads from the current replica set primary node. For more information, see the
217- :rapid:`Read Preference Server Manual entry </core/read-preference/#mongodb-readmode-primary>`.
271+ :rapid:`Read Preference Server manual entry </core/read-preference/#mongodb-readmode-primary>`.
218272
219273 * - ``PrimaryPreferred()``
220274 - The client reads from the primary node in most situations. If the primary is
221275 unavailable, operations read from secondary members. For more
222- information, see the :rapid:`Read Preference Server Manual entry </core/read-preference/#mongodb-readmode-primaryPreferred>`.
276+ information, see the :rapid:`Read Preference Server manual entry </core/read-preference/#mongodb-readmode-primaryPreferred>`.
223277
224278 * - ``Secondary()``
225279 - The client reads from the secondary members of the replica set. For more information, see the
226- :rapid:`Read Preference Server Manual entry </core/read-preference/#mongodb-readmode-secondary>`.
280+ :rapid:`Read Preference Server manual entry </core/read-preference/#mongodb-readmode-secondary>`.
227281
228282 * - ``SecondaryPreferred()``
229283 - The client reads from the secondary nodes in most situations. If the secondaries are
230284 unavailable, operations read from the primary member. For more information, see the
231- :rapid:`Read Preference Server Manual entry </core/read-preference/#mongodb-readmode-secondaryPreferred>`.
285+ :rapid:`Read Preference Server manual entry </core/read-preference/#mongodb-readmode-secondaryPreferred>`.
232286
233287.. tip::
234288
235289 You can alternatively specify a read preference in your connection
236- string. See the :manual:`Server Manual entry on Read Preference
290+ string. See the :manual:`Server manual entry on Read Preference
237291 Options </reference/connection-string/#read-preference-options>` for
238292 more information.
239293
@@ -265,7 +319,6 @@ Server documentation:
265319API Documentation
266320~~~~~~~~~~~~~~~~~
267321
268- - `Option <{+api+}/mongo/writeconcern#Option>`__
269322- `WriteConcern <{+api+}/mongo/writeconcern#WriteConcern>`__
270323- `ReadConcern <{+api+}/mongo/readconcern#ReadConcern>`__
271324- `ReadPref <{+api+}/mongo/readpref#ReadPref>`__
0 commit comments