@@ -37,6 +37,11 @@ current inventory:
37
37
{ "_id": 4, "color": "green", "qty": 6 }
38
38
{ "_id": 5, "color": "pink", "qty": 0 }
39
39
40
+ This data is modeled with the following Kotlin data class:
41
+
42
+ .. literalinclude:: /examples/generated/ChangeTest.snippet.data-model.kt
43
+ :language: kotlin
44
+
40
45
.. _update-operation:
41
46
42
47
.. _kotlin-fundamentals-update:
@@ -48,20 +53,20 @@ Update operations can change fields and values. They apply changes
48
53
specified in an update document to one or more documents that match your
49
54
query filter.
50
55
51
- The `updateOne() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#updateOne(org.bson.conversions.Bson,org.bson.conversions.Bson )>`__
56
+ The `updateOne() <TODO:(DOCSP-29169 )>`__
52
57
method changes the first document your query filter matches and the
53
- `updateMany() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#updateMany(org.bson.conversions.Bson,org.bson.conversions.Bson )>`__
58
+ `updateMany() <TODO:(DOCSP-29169 )>`__
54
59
method changes all the documents your query filter matches.
55
60
56
61
You can call the ``updateOne()`` and ``updateMany()`` methods on a
57
62
``MongoCollection`` instance as follows:
58
63
59
- .. code-block:: java
60
-
61
- collection.updateOne(query, updateDocument);
64
+ .. code-block:: kotlin
62
65
63
- collection.updateMany (query, updateDocument);
66
+ collection.updateOne (query, updateDocument)
64
67
68
+ collection.updateMany(query, updateDocument)
69
+
65
70
Update Operation Parameters
66
71
~~~~~~~~~~~~~~~~~~~~~~~~~~~
67
72
@@ -74,38 +79,55 @@ parameters:
74
79
You can create the ``updateDocument`` using an ``Updates`` builder as
75
80
follows:
76
81
77
- .. code-block:: java
82
+ .. code-block:: kotlin
78
83
79
- Bson updateDocument = Updates.operator(field, value);
84
+ val updateDocument = Updates.operator(field, value)
80
85
81
86
See the MongoDB API documentation for a `complete list of
82
- Updates builders and their usage <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Updates.html >`__.
87
+ Updates builders and their usage <TODO:(DOCSP-29169) >`__.
83
88
84
89
Example
85
90
```````
86
91
87
- The paint store receives a fresh shipment and needs to update their inventory.
88
- The shipment contains 20 cans of each paint color.
92
+ The paint store needs to update their inventory after a customer returns a
93
+ can of yellow paint.
94
+
95
+ To update the single can of paint, call the ``updateOne()`` method specifying
96
+ the following:
97
+
98
+ - A query filter that matches the yellow color
99
+ - An update document that contains instructions to increment the ``qty`` field by "1"
100
+
101
+ .. io-code-block::
102
+
103
+ .. input:: /examples/generated/ChangeTest.snippet.update-one.kt
104
+ :language: kotlin
105
+
106
+ .. output::
107
+ :language: console
108
+
109
+ Matched document count: 1
110
+ Modified document count: 1
111
+
112
+ The paint store then receives a fresh shipment and needs to update their
113
+ inventory again. The shipment contains 20 cans of each paint color.
89
114
90
115
To update the inventory, call the ``updateMany()`` method specifying the
91
116
following:
92
117
93
118
- A query filter that matches all the colors
94
119
- An update document that contains instructions to increment the ``qty`` field by "20"
95
120
96
- .. literalinclude:: /includes/fundamentals/code-snippets/Update.java
97
- :language: java
98
- :dedent:
99
- :start-after: begin updateManyExample
100
- :end-before: end updateManyExample
121
+ .. io-code-block::
101
122
102
- Your output should look something like this:
123
+ .. input:: /examples/generated/ChangeTest.snippet.update-many.kt
124
+ :language: kotlin
103
125
104
- .. code-block:: none
105
- :copyable: false
126
+ .. output::
127
+ :language: console
106
128
107
- Matched document count: 5
108
- Modified document count: 5
129
+ Matched document count: 5
130
+ Modified document count: 5
109
131
110
132
The following shows the updated documents in the ``paint_inventory`` collection:
111
133
@@ -140,17 +162,17 @@ A replace operation substitutes one document from your collection. The
140
162
substitution occurs between a document your query filter matches and a
141
163
replacement document.
142
164
143
- The `replaceOne() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#replaceOne(org.bson.conversions.Bson,TDocument )>`__
165
+ The `replaceOne() <TODO:(DOCSP-29169 )>`__
144
166
method removes all the existing fields and values in the
145
167
matching document (except the ``_id`` field) and substitutes it with the
146
168
replacement document.
147
169
148
170
You can call the ``replaceOne()`` method on a ``MongoCollection``
149
171
instance as follows:
150
172
151
- .. code-block:: java
173
+ .. code-block:: kotlin
152
174
153
- collection.replaceOne(query, replacementDocument);
175
+ collection.replaceOne(query, replacementDocument)
154
176
155
177
Replace Operation Parameters
156
178
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -172,19 +194,16 @@ following:
172
194
- A query filter that matches documents where the ``color`` is "pink"
173
195
- A replacement document where the ``color`` is "orange" and the ``qty`` is "25"
174
196
175
- .. literalinclude:: /includes/fundamentals/code-snippets/Update.java
176
- :language: java
177
- :dedent:
178
- :start-after: begin replaceOneExample
179
- :end-before: end replaceOneExample
197
+ .. io-code-block::
180
198
181
- Your output should look something like this:
199
+ .. input:: /examples/generated/ChangeTest.snippet.replace-one.kt
200
+ :language: kotlin
182
201
183
- .. code-block:: none
184
- :copyable: false
202
+ .. output::
203
+ :language: console
185
204
186
- Matched document count: 1
187
- Modified document count: 1
205
+ Matched document count: 1
206
+ Modified document count: 1
188
207
189
208
The following shows the updated document:
190
209
0 commit comments