4
4
Modify Execution of CRUD Operations
5
5
===================================
6
6
7
- .. default-domain:: mongodb
8
-
9
7
.. contents:: On this page
10
8
:local:
11
9
:backlinks: none
@@ -34,6 +32,8 @@ the following levels:
34
32
You should read this guide if you need to customize the consistency and
35
33
availability of the data in your replica sets.
36
34
35
+ .. _golang-writeconcern:
36
+
37
37
Write Concern
38
38
-------------
39
39
@@ -49,9 +49,9 @@ Options
49
49
50
50
The {+driver-long+} provides the ``writeconcern`` package, which lets
51
51
you 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 :
55
55
56
56
.. list-table::
57
57
:widths: 25 75
@@ -60,56 +60,110 @@ write concern:
60
60
* - Method
61
61
- Description
62
62
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->`.
67
68
|
68
- | **Parameter**: ``bool ``
69
+ | **Parameter**: ``string ``
69
70
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 >`.
74
75
|
75
- | **Parameter**: ``int``
76
+ | **Parameter**: none
76
77
77
- * - ``WMajority ()``
78
+ * - ``Majority ()``
78
79
- | 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
80
81
:rapid:`Write Concern specification
81
82
</reference/write-concern/#mongodb-writeconcern-writeconcern.-majority->`.
82
83
|
83
84
| **Parameter**: none
84
85
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 ->`.
90
91
|
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
+
93
137
.. tip::
94
138
95
139
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
97
141
</reference/connection-string/#write-concern-options>` for more information.
98
142
99
143
Example
100
144
~~~~~~~
101
145
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.
105
151
106
152
.. code-block:: go
107
- :emphasize-lines: 2-3
153
+ :emphasize-lines: 2-6,10-11
108
154
109
155
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)
113
167
114
168
Read Concern
115
169
------------
@@ -210,30 +264,30 @@ type has the following methods to specify the read preference:
210
264
* - ``Nearest()``
211
265
- The client reads from a random eligible replica set member,
212
266
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>`.
214
268
215
269
* - ``Primary()``
216
270
- 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>`.
218
272
219
273
* - ``PrimaryPreferred()``
220
274
- The client reads from the primary node in most situations. If the primary is
221
275
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>`.
223
277
224
278
* - ``Secondary()``
225
279
- 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>`.
227
281
228
282
* - ``SecondaryPreferred()``
229
283
- The client reads from the secondary nodes in most situations. If the secondaries are
230
284
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>`.
232
286
233
287
.. tip::
234
288
235
289
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
237
291
Options </reference/connection-string/#read-preference-options>` for
238
292
more information.
239
293
@@ -265,7 +319,6 @@ Server documentation:
265
319
API Documentation
266
320
~~~~~~~~~~~~~~~~~
267
321
268
- - `Option <{+api+}/mongo/writeconcern#Option>`__
269
322
- `WriteConcern <{+api+}/mongo/writeconcern#WriteConcern>`__
270
323
- `ReadConcern <{+api+}/mongo/readconcern#ReadConcern>`__
271
324
- `ReadPref <{+api+}/mongo/readpref#ReadPref>`__
0 commit comments