@@ -170,4 +170,119 @@ documents match.
170
170
to a document that violate unique index constraints on the
171
171
collection. For more information about constraints on unique indexes,
172
172
see :manual:`Unique Indexes </core/index-unique/>` in the
173
- {+mdb-server+} manual.
173
+ {+mdb-server+} manual.
174
+
175
+ Update Example
176
+ ~~~~~~~~~~~~~~
177
+
178
+ The following code is a complete, standalone file that performs an update one
179
+ operation and an update many operation.
180
+
181
+ .. include:: /includes/crud/example-intro.rst
182
+
183
+ .. io-code-block::
184
+
185
+ .. input:: /includes/crud/Insert.java
186
+ :language: java
187
+ :dedent:
188
+
189
+ .. output::
190
+ :language: none
191
+ :visible: false
192
+
193
+ InsertOne document id: BsonObjectId{value=...}
194
+ InsertMany document ids: {0=BsonObjectId{value=...}, 1=BsonObjectId{value=...}}
195
+
196
+ .. _replace-operation:
197
+
198
+ Replace
199
+ -------
200
+
201
+ A replace operation substitutes one document from your collection. The
202
+ substitution occurs between a document your query filter matches and a
203
+ replacement document.
204
+
205
+ The `replaceOne() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#replaceOne(org.bson.conversions.Bson,TDocument)>`__
206
+ method removes all the existing fields and values in the
207
+ matching document (except the ``_id`` field) and substitutes it with the
208
+ replacement document.
209
+
210
+ You can call the ``replaceOne()`` method on a ``MongoCollection``
211
+ instance as follows:
212
+
213
+ .. code-block:: java
214
+
215
+ collection.replaceOne(<query>, <replacement>);
216
+
217
+ Replace Operation Parameters
218
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
219
+
220
+ The ``replaceOne()`` method has the following parameters:
221
+
222
+ - ``query`` specifies a query filter with the criteria to match a
223
+ document to replace in your collection.
224
+ - ``replacement`` specifies fields and values of a new ``Document``
225
+ object to replace the matched document.
226
+ - *(Optional)* ``replaceOptions`` specifies options that you can set to
227
+ customize how the driver performs the replace operation. To learn more
228
+ about this type, see the API documentation for `ReplaceOptions
229
+ <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/ReplaceOptions.html>`__.
230
+
231
+ Replace One Example
232
+ ~~~~~~~~~~~~~~~~~~~
233
+
234
+ The paint store realizes they must update their inventory again. What they
235
+ thought was 20 cans of pink paint is actually 25 cans of orange paint.
236
+
237
+ To update the inventory, call the ``replaceOne()`` method specifying the
238
+ following:
239
+
240
+ - A query filter that matches documents where the ``color`` is "pink"
241
+ - A replacement document where the ``color`` is "orange" and the ``qty`` is "25"
242
+
243
+ .. literalinclude:: /includes/fundamentals/code-snippets/Update.java
244
+ :language: java
245
+ :dedent:
246
+ :start-after: begin replaceOneExample
247
+ :end-before: end replaceOneExample
248
+
249
+ The output of the preceding code resembles the following:
250
+
251
+ .. code-block:: none
252
+ :copyable: false
253
+
254
+ Matched document count: 1
255
+ Modified document count: 1
256
+
257
+ The following shows the updated document:
258
+
259
+ .. code-block:: json
260
+ :copyable: false
261
+
262
+ { "_id": 5, "color": "orange", "qty": 25 }
263
+
264
+ If multiple documents match the query filter specified in
265
+ the ``replaceOne()`` method, it replaces the first result. You can
266
+ specify a sort in a ``ReplaceOptions`` instance to apply an order to
267
+ matched documents before the server performs the replace operation, as
268
+ shown in the following code:
269
+
270
+ .. literalinclude:: /includes/fundamentals/code-snippets/Update.java
271
+ :language: java
272
+ :dedent:
273
+ :start-after: begin replaceoptions
274
+ :end-before: end replaceoptions
275
+
276
+ If zero documents match the query filter in the replace operation,
277
+ ``replaceOne()`` makes no changes to documents in the collection. See
278
+ our :ref:`upsert guide <java-upsert>` to
279
+ learn how to insert a new document instead of replacing one if no
280
+ documents match.
281
+
282
+ .. important::
283
+
284
+ The ``replaceOne()`` method cannot make changes to a document that
285
+ violate unique index constraints on the collection.
286
+ For more information about constraints on unique indexes,
287
+ see :manual:`Unique Indexes </core/index-unique/>` in the
288
+ {+mdb-server+} manual.
0 commit comments