Skip to content

Commit 2e948b3

Browse files
committed
DOCSP-34105: Update by ID example (#79)
(cherry picked from commit 5548a92)
1 parent 4c118be commit 2e948b3

File tree

2 files changed

+98
-18
lines changed
  • source

2 files changed

+98
-18
lines changed

source/fundamentals/crud/write-operations/change.txt

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ The {+driver-short+} provides the following methods to change documents:
6464
You can retrieve and modify data in one action by using compound
6565
operations. To learn more, see the guide on :ref:`rust-compound-operations`.
6666

67+
.. _rust-id:
68+
6769
The _id Field
6870
~~~~~~~~~~~~~
6971

@@ -165,22 +167,22 @@ The following documents describe employees of a company:
165167
:copyable: false
166168

167169
{
168-
"_id": { ... },
170+
"_id": ObjectId('4337'),
169171
"name": "Shelley Olson",
170172
"department": "Marketing",
171173
"role": "Director",
172174
"bonus": 3000
173175
},
174176
{
175-
"_id": { ... },
177+
"_id": ObjectId('4902'),
176178
"name": "Remi Ibrahim",
177179
"department": "Marketing",
178180
"role": "Consultant",
179181
"bonus": 1800
180182
}
181183

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:
184186

185187
- A query filter to match documents where the value of the
186188
``department`` field is ``"Marketing"``
@@ -204,28 +206,86 @@ This example performs an update operation with the
204206
:language: console
205207
:visible: false
206208

207-
Modified 2 document(s)
209+
Modified documents: 2
208210

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:
210212

211213
.. code-block:: json
212214
:copyable: false
213215

214216
{
215-
"_id": { ... },
217+
"_id": ObjectId('4337'),
216218
"name": "Shelley Olson",
217219
"department": "Business Operations",
218220
"role": "Analytics Specialist",
219221
"bonus": 3500
220222
},
221223
{
222-
"_id": { ... },
224+
"_id": ObjectId('4902'),
223225
"name": "Remi Ibrahim",
224226
"department": "Business Operations",
225227
"role": "Analytics Specialist",
226228
"bonus": 2300
227229
}
228230

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+
229289
.. _rust-replace-document:
230290

231291
Replace a Document
@@ -290,7 +350,7 @@ The following document describes an employee of a company:
290350
:copyable: false
291351

292352
{
293-
"_id": 4501,
353+
"_id": ObjectId('4501'),
294354
"name": "Matt DeGuy",
295355
"role": "Consultant",
296356
"team_members": [ "Jill Gillison", "Susan Lee" ]
@@ -315,8 +375,8 @@ the preceding document with one that has the following fields:
315375
:language: console
316376
:visible: false
317377

318-
Matched 1 document(s)
319-
Modified 1 document(s)
378+
Matched documents: 1
379+
Modified documents: 1
320380

321381
The replaced document contains the contents of the replacement document
322382
and the immutable ``_id`` field:
@@ -325,7 +385,7 @@ and the immutable ``_id`` field:
325385
:copyable: false
326386

327387
{
328-
"_id": 4501,
388+
"_id": ObjectId('4501'),
329389
"name": "Susan Lee",
330390
"role": "Lead Consultant",
331391
"team_members": [ "Jill Gillison" ]

source/includes/fundamentals/code-snippets/crud/change.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
use bson::Document;
2-
use mongodb::{bson::doc, options::UpdateOptions, Client, Collection};
2+
use mongodb::{
3+
bson::{doc, oid::ObjectId},
4+
options::UpdateOptions,
5+
Client,
6+
Collection
7+
};
8+
use std::str::FromStr;
39
use std::env;
410

511
#[tokio::main]
@@ -11,17 +17,31 @@ async fn main() -> mongodb::error::Result<()> {
1117

1218
// begin-update
1319
let update_doc = doc! {
14-
"$set": doc!{ "department": "Business Operations",
20+
"$set": doc! { "department": "Business Operations",
1521
"role": "Analytics Specialist" },
16-
"$inc": doc!{ "bonus": 500 }
22+
"$inc": doc! { "bonus": 500 }
1723
};
1824

1925
let res = my_coll
2026
.update_many(doc! { "department": "Marketing" }, update_doc, None)
2127
.await?;
22-
println!("Modified {} document(s)", res.modified_count);
28+
println!("Modified documents: {}", res.modified_count);
2329
// end-update
2430

31+
// begin-update-by-id
32+
let id = ObjectId::from_str("4274").expect("Could not convert to ObjectId");
33+
let filter_doc = doc! { "_id": id };
34+
35+
let update_doc = doc! {
36+
"$set": doc! { "name": "Jill Gillison" }
37+
};
38+
39+
let res = my_coll
40+
.update_one(filter_doc, update_doc, None)
41+
.await?;
42+
println!("Modified documents: {}", res.modified_count);
43+
// end-update-by-id
44+
2545
// begin-replace
2646
let replace_doc = doc! {
2747
"name": "Susan Lee",
@@ -30,10 +50,10 @@ async fn main() -> mongodb::error::Result<()> {
3050
};
3151

3252
let res = my_coll
33-
.replace_one(doc! { "_id": 4501 }, replace_doc, None)
53+
.replace_one(doc! { "name": "Matt DeGuy" }, replace_doc, None)
3454
.await?;
3555
println!(
36-
"Matched {} document(s)\nModified {} document(s)",
56+
"Matched documents: {}\nModified documents: {}",
3757
res.matched_count, res.modified_count
3858
);
3959
// end-replace

0 commit comments

Comments
 (0)