@@ -64,6 +64,8 @@ The {+driver-short+} provides the following methods to change documents:
64
64
You can retrieve and modify data in one action by using compound
65
65
operations. To learn more, see the guide on :ref:`rust-compound-operations`.
66
66
67
+ .. _rust-id:
68
+
67
69
The _id Field
68
70
~~~~~~~~~~~~~
69
71
@@ -165,22 +167,22 @@ The following documents describe employees of a company:
165
167
:copyable: false
166
168
167
169
{
168
- "_id": { ... } ,
170
+ "_id": ObjectId('4337') ,
169
171
"name": "Shelley Olson",
170
172
"department": "Marketing",
171
173
"role": "Director",
172
174
"bonus": 3000
173
175
},
174
176
{
175
- "_id": { ... } ,
177
+ "_id": ObjectId('4902') ,
176
178
"name": "Remi Ibrahim",
177
179
"department": "Marketing",
178
180
"role": "Consultant",
179
181
"bonus": 1800
180
182
}
181
183
182
- This example performs an update operation with the
183
- ``update_many()`` method. The method has the following parameters:
184
+ This example performs an update operation with the ``update_many()`` method.
185
+ The ``update_many()`` method takes the following parameters:
184
186
185
187
- A query filter to match documents where the value of the
186
188
``department`` field is ``"Marketing"``
@@ -204,28 +206,86 @@ This example performs an update operation with the
204
206
:language: console
205
207
:visible: false
206
208
207
- Modified 2 document(s)
209
+ Modified documents: 2
208
210
209
- The following shows the updated documents resulting from the preceding update operation:
211
+ The following documents reflect the changes resulting from the preceding update operation:
210
212
211
213
.. code-block:: json
212
214
:copyable: false
213
215
214
216
{
215
- "_id": { ... } ,
217
+ "_id": ObjectId('4337') ,
216
218
"name": "Shelley Olson",
217
219
"department": "Business Operations",
218
220
"role": "Analytics Specialist",
219
221
"bonus": 3500
220
222
},
221
223
{
222
- "_id": { ... } ,
224
+ "_id": ObjectId('4902') ,
223
225
"name": "Remi Ibrahim",
224
226
"department": "Business Operations",
225
227
"role": "Analytics Specialist",
226
228
"bonus": 2300
227
229
}
228
230
231
+ Update by ObjectId Example
232
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
233
+
234
+ The following document describes an employee of a company:
235
+
236
+ .. code-block:: json
237
+ :copyable: false
238
+ :emphasize-lines: 2
239
+
240
+ {
241
+ "_id": ObjectId('4274'),
242
+ "name": "Jill Millerton",
243
+ "department": "Marketing",
244
+ "role": "Consultant"
245
+ }
246
+
247
+ This example queries for the preceding document by specifying a query filter to match the
248
+ document's unique ``_id`` value. Then, the code performs an update operation with the
249
+ ``update_one()`` method. The ``update_one()`` method takes the following parameters:
250
+
251
+ - Query filter that matches a document in which the value of the ``_id`` field is
252
+ ``ObjectId('4274')``
253
+
254
+ - Update document that creates instructions to set the value of ``name`` to
255
+ ``"Jill Gillison"``
256
+
257
+ .. io-code-block::
258
+
259
+ .. input:: /includes/fundamentals/code-snippets/crud/change.rs
260
+ :start-after: begin-update-by-id
261
+ :end-before: end-update-by-id
262
+ :language: rust
263
+ :dedent:
264
+
265
+ .. output::
266
+ :language: console
267
+ :visible: false
268
+
269
+ Modified documents: 1
270
+
271
+ The following document reflects the changes resulting from the preceding update operation:
272
+
273
+ .. code-block:: json
274
+ :copyable: false
275
+
276
+ {
277
+ "_id": ObjectId('4274'),
278
+ "name": "Jill Gillison",
279
+ "department": "Marketing",
280
+ "role": "Consultant"
281
+ }
282
+
283
+ .. tip::
284
+
285
+ To learn more about the ``_id`` field, see the :ref:`_id Field <rust-id>`
286
+ section of this page or the :manual:`ObjectId() </reference/method/ObjectId/>`
287
+ method documentation in the Server manual.
288
+
229
289
.. _rust-replace-document:
230
290
231
291
Replace a Document
@@ -290,7 +350,7 @@ The following document describes an employee of a company:
290
350
:copyable: false
291
351
292
352
{
293
- "_id": 4501,
353
+ "_id": ObjectId(' 4501') ,
294
354
"name": "Matt DeGuy",
295
355
"role": "Consultant",
296
356
"team_members": [ "Jill Gillison", "Susan Lee" ]
@@ -315,8 +375,8 @@ the preceding document with one that has the following fields:
315
375
:language: console
316
376
:visible: false
317
377
318
- Matched 1 document(s)
319
- Modified 1 document(s)
378
+ Matched documents: 1
379
+ Modified documents: 1
320
380
321
381
The replaced document contains the contents of the replacement document
322
382
and the immutable ``_id`` field:
@@ -325,7 +385,7 @@ and the immutable ``_id`` field:
325
385
:copyable: false
326
386
327
387
{
328
- "_id": 4501,
388
+ "_id": ObjectId(' 4501') ,
329
389
"name": "Susan Lee",
330
390
"role": "Lead Consultant",
331
391
"team_members": [ "Jill Gillison" ]
0 commit comments