File tree Expand file tree Collapse file tree 4 files changed +145
-4
lines changed
includes/usage-examples/code-snippets Expand file tree Collapse file tree 4 files changed +145
-4
lines changed Original file line number Diff line number Diff line change
1
+ use std:: env;
2
+ use mongodb:: { bson:: doc, Client , Collection } ;
3
+ use serde:: { Deserialize , Serialize } ;
4
+
5
+ #[ derive( Serialize , Deserialize , Debug ) ]
6
+ struct Restaurant {
7
+ borough : String ,
8
+ cuisine : String ,
9
+ name : String ,
10
+ }
11
+
12
+ #[ tokio:: main]
13
+ async fn main ( ) -> mongodb:: error:: Result < ( ) > {
14
+ let uri = "<connection string>" ;
15
+
16
+ let client = Client :: with_uri_str ( uri) . await ?;
17
+ let my_coll: Collection < Restaurant > = client
18
+ . database ( "sample_restaurants" )
19
+ . collection ( "restaurants" ) ;
20
+
21
+ let filter = doc ! { "name" : "Landmark Coffee Shop" } ;
22
+ let replacement = Restaurant {
23
+ borough : "Brooklyn" . to_string ( ) ,
24
+ cuisine : "Café/Coffee/Tea" . to_string ( ) ,
25
+ name : "Harvest Moon Café" . to_string ( ) ,
26
+ } ;
27
+
28
+ let res = my_coll. replace_one ( filter, replacement, None ) . await ?;
29
+ println ! (
30
+ "Matched documents: {}\n Replaced documents: {}" ,
31
+ res. matched_count,
32
+ res. modified_count
33
+ ) ;
34
+
35
+ Ok ( ( ) )
36
+ }
Original file line number Diff line number Diff line change
1
+ use std:: env;
2
+ use mongodb:: { bson:: doc, sync:: { Client , Collection } } ;
3
+ use serde:: { Deserialize , Serialize } ;
4
+
5
+ #[ derive( Serialize , Deserialize , Debug ) ]
6
+ struct Restaurant {
7
+ borough : String ,
8
+ cuisine : String ,
9
+ name : String ,
10
+ }
11
+
12
+ fn main ( ) -> mongodb:: error:: Result < ( ) > {
13
+ let uri = "<connection string>" ;
14
+
15
+ let client = Client :: with_uri_str ( uri) ?;
16
+ let my_coll: Collection < Restaurant > = client
17
+ . database ( "sample_restaurants" )
18
+ . collection ( "restaurants" ) ;
19
+
20
+ let filter = doc ! { "name" : "Landmark Coffee Shop" } ;
21
+ let replacement = Restaurant {
22
+ borough : "Brooklyn" . to_string ( ) ,
23
+ cuisine : "Café/Coffee/Tea" . to_string ( ) ,
24
+ name : "Harvest Moon Café" . to_string ( ) ,
25
+ } ;
26
+
27
+ let res = my_coll. replace_one ( filter, replacement, None ) ?;
28
+ println ! (
29
+ "Matched documents: {}\n Replaced documents: {}" ,
30
+ res. matched_count,
31
+ res. modified_count
32
+ ) ;
33
+
34
+ Ok ( ( ) )
35
+ }
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ Usage Examples
13
13
.. toctree::
14
14
15
15
/usage-examples/find
16
+ /usage-examples/replace
16
17
/usage-examples/deleteMany
17
18
/usage-examples/count
18
19
@@ -89,6 +90,7 @@ Available Usage Examples
89
90
------------------------
90
91
91
92
- :ref:`Find Multiple Documents <rust-find-usage>`
93
+ - :ref:`Replace a Document <rust-replace-usage>`
92
94
- :ref:`Delete Multiple Documents <rust-delete-many-usage>`
93
95
- :ref:`Count Documents <rust-count-usage>`
94
96
@@ -98,8 +100,5 @@ Available Usage Examples
98
100
- :ref:`Insert Multiple Documents <rust-insert-many-usage>`
99
101
- :ref:`Update a Document <rust-update-one-usage>`
100
102
- :ref:`Update Multiple Documents <rust-update-many-usage>`
101
- - :ref:`Replace a Document <rust-replace-usage>`
102
103
- :ref:`Delete a Document <rust-delete-one-usage>`
103
- - :ref:`Find Distinct Values <rust-distinct-usage>`
104
-
105
-
104
+ - :ref:`Find Distinct Values <rust-distinct-usage>`
Original file line number Diff line number Diff line change
1
+ .. _rust-replace-usage:
2
+
3
+ ==================
4
+ Replace a Document
5
+ ==================
6
+
7
+ You can replace a document in a collection by calling the `replace_one()
8
+ <{+api+}/struct.Collection.html#method.replace_one>`__ method on a
9
+ ``Collection`` instance.
10
+
11
+ Pass the following parameters to the ``replace_one()`` method to update
12
+ a document:
13
+
14
+ - Query filter, which specifies the criteria to match
15
+ - Replacement document, which contains the fields and values that will
16
+ replace an existing document
17
+
18
+ The ``replace_one()`` method returns an `UpdateResult
19
+ <{+api+}/results/struct.UpdateResult.html>`__ type which contains
20
+ information about the result of the replace operation, such as the
21
+ number of modified documents.
22
+
23
+ To learn more about the ``replace_one()`` method, see the
24
+ :ref:`rust-replace-document` section of the Modify Documents guide.
25
+
26
+ Example
27
+ -------
28
+
29
+ This example replaces a document in the ``restaurants`` collection of
30
+ the ``sample_restaurants`` database. The example uses a ``Restaurant``
31
+ struct that has ``name``, ``borough``, and ``cuisine`` fields to model
32
+ documents in the collection.
33
+
34
+ The following code replaces a document in which the value of the
35
+ ``name`` field is ``"Landmark Coffee Shop"`` with a new document:
36
+
37
+ .. tabs::
38
+
39
+ .. tab:: Asynchronous
40
+ :tabid: find-async
41
+
42
+ .. io-code-block::
43
+ :copyable: true
44
+
45
+ .. input:: /includes/usage-examples/code-snippets/replace-async.rs
46
+ :language: rust
47
+ :dedent:
48
+
49
+ .. output::
50
+ :language: console
51
+ :visible: false
52
+
53
+ Matched documents: 1
54
+ Replaced documents: 1
55
+
56
+ .. tab:: Synchronous
57
+ :tabid: find-sync
58
+
59
+ .. io-code-block::
60
+ :copyable: true
61
+
62
+ .. input:: /includes/usage-examples/code-snippets/replace-sync.rs
63
+ :language: rust
64
+ :dedent:
65
+
66
+ .. output::
67
+ :language: console
68
+ :visible: false
69
+
70
+ Matched documents: 1
71
+ Replaced documents: 1
You can’t perform that action at this time.
0 commit comments