Skip to content

Commit 815dd63

Browse files
committed
DOCSP-46124: sort option for updateone and replaceone
1 parent 0978b9d commit 815dd63

File tree

3 files changed

+90
-17
lines changed

3 files changed

+90
-17
lines changed

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

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Update
4444

4545
Update operations can modify fields and values. They apply changes
4646
specified in an update document to one or more documents that match your
47-
query filter.
47+
query filter.
4848

4949
The `updateOne() <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoCollection.html#updateOne(org.bson.conversions.Bson,org.bson.conversions.Bson)>`__
5050
method changes the first document your query filter matches and the
@@ -66,30 +66,62 @@ Update Operation Parameters
6666
The ``updateOne()`` and ``updateMany()`` methods both have the following
6767
parameters:
6868

69-
- ``query`` specifies a query filter with the criteria to match documents to update in your collection
70-
- ``updateDocument`` specifies the fields and values to modify in the matching document or documents. For this example, we use the :doc:`Updates builder </fundamentals/builders/updates>` to create the update document.
69+
- ``query`` specifies a query filter with the criteria to match
70+
documents to update in your collection.
71+
- ``update`` specifies the fields and values to modify in the matching
72+
document or documents. For this example, we use the :doc:`Updates
73+
builder </fundamentals/builders/updates>` to create the update
74+
document.
75+
- *(Optional)* ``updateOptions`` specifies options that you can set to
76+
customize how the driver performs the update operation.
7177

7278
You can create the ``updateDocument`` using an ``Updates`` builder as
7379
follows:
7480

7581
.. code-block:: java
7682

77-
Bson updateDocument = Updates.operator(field, value);
83+
Bson updateDocument = Updates.operator(<field>, <value>);
7884

7985
See the MongoDB API documentation for a `complete list of
80-
Updates builders and their usage <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Updates.html>`__.
86+
Updates builders and their usage
87+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Updates.html>`__.
8188

82-
Example
83-
```````
89+
Update One Example
90+
~~~~~~~~~~~~~~~~~~
91+
92+
The following example demonstrates how to change the value of the
93+
``color`` field in the first matching document in which the value of
94+
``qty`` is ``0``:
95+
96+
.. literalinclude:: /includes/fundamentals/code-snippets/Update.java
97+
:language: java
98+
:dedent:
99+
:start-after: begin updateOneExample
100+
:end-before: end updateOneExample
101+
102+
If multiple documents match the query filter specified in
103+
the ``updateOne()`` method, it replaces the first result. You can
104+
specify a sort in an ``UpdateOptions`` instance to apply an order to
105+
matched documents before the driver performs the replace operation, as
106+
shown in the following code:
107+
108+
.. literalinclude:: /includes/fundamentals/code-snippets/Update.java
109+
:language: java
110+
:dedent:
111+
:start-after: begin updateoptions
112+
:end-before: end updateoptions
113+
114+
Update Many Example
115+
~~~~~~~~~~~~~~~~~~~
84116

85117
The paint store receives a fresh shipment and needs to update their inventory.
86118
The shipment contains 20 cans of each paint color.
87119

88120
To update the inventory, call the ``updateMany()`` method specifying the
89121
following:
90122

91-
- A query filter that matches all the colors
92-
- An update document that contains instructions to increment the ``qty`` field by "20"
123+
- A query filter that matches all the colors
124+
- An update document that contains instructions to increment the ``qty`` field by ``20``
93125

94126
.. literalinclude:: /includes/fundamentals/code-snippets/Update.java
95127
:language: java
@@ -157,10 +189,13 @@ Replace Operation Parameters
157189
The ``replaceOne()`` method has the following parameters:
158190

159191
- ``query`` specifies a query filter with the criteria to match a document to replace in your collection
160-
- ``replacementDocument`` specifies fields and values of a new ``Document`` object to replace in the matched document
192+
- ``replacement`` specifies fields and values of a new ``Document``
193+
object to replace in the matched document
194+
- *(Optional)* ``replaceOptions`` specifies options that you can set to
195+
customize how the driver performs the replace operation
161196

162-
Example
163-
```````
197+
Replace One Example
198+
~~~~~~~~~~~~~~~~~~~
164199

165200
The paint store realizes they must update their inventory again. What they
166201
thought was 20 cans of pink paint is actually 25 cans of orange paint.
@@ -192,14 +227,23 @@ The following shows the updated document:
192227

193228
{ "_id": 5, "color": "orange", "qty": 25 }
194229

230+
If multiple documents match the query filter specified in
231+
the ``replaceOne()`` method, it replaces the first result. You can
232+
specify a sort in a ``ReplaceOptions`` instance to apply an order to
233+
matched documents before the driver performs the replace operation, as
234+
shown in the following code:
235+
236+
.. literalinclude:: /includes/fundamentals/code-snippets/Update.java
237+
:language: java
238+
:dedent:
239+
:start-after: begin replaceoptions
240+
:end-before: end replaceoptions
241+
195242
If zero documents match the query filter in the replace operation,
196243
``replaceOne()`` makes no changes to documents in the collection. See
197244
our :doc:`upsert guide </fundamentals/crud/write-operations/upsert>` to
198245
learn how to insert a new document instead of replacing one if no
199-
documents match.
200-
201-
If multiple documents match the query filter specified in
202-
the ``replaceOne()`` method, it replaces the first result.
246+
documents match.
203247

204248
.. important::
205249

@@ -208,4 +252,3 @@ the ``replaceOne()`` method, it replaces the first result.
208252
For more information about constraints on unique indexes,
209253
see :manual:`Unique Indexes </core/index-unique/>` in the
210254
{+mdb-server+} manual.
211-

source/includes/fundamentals/code-snippets/Update.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.mongodb.client.model.Updates;
1515
import com.mongodb.client.result.UpdateResult;
1616

17+
import static com.mongodb.client.model.Sorts.ascending;
18+
1719
public class Update {
1820
private final MongoCollection<Document> collection;
1921
private final MongoClient mongoClient;
@@ -41,6 +43,25 @@ public static void main(String [] args){
4143
update.preview(false);
4244
}
4345

46+
private void updateOneExample(){
47+
// Creates a filter and update document to change the value of ``color``
48+
// begin updateOneExample
49+
Bson filter = Filters.eq("qty", "0");
50+
Bson update = Updates.set("color", "dandelion");
51+
52+
// Updates first document and prints the number of matched and modified documents
53+
UpdateResult result = collection.updateOne(filter, update);
54+
// end updateOneExample
55+
56+
System.out.println("Matched document count: " + result.getMatchedCount());
57+
System.out.println("Modified document count: " + result.getModifiedCount());
58+
59+
// begin updateoptions
60+
UpdateOptions options = UpdateOptions.sort(ascending("color"));
61+
UpdateResult result = collection.updateOne(filter, document, options);
62+
// end updateoptions
63+
}
64+
4465
private void updateManyExample(){
4566
// Creates a filter and update document to increase the "qty" value of all documents
4667
// begin updateManyExample
@@ -67,6 +88,11 @@ private void replaceOneExample(){
6788
System.out.println("Matched document count: " + result.getMatchedCount());
6889
System.out.println("Modified document count: " + result.getModifiedCount());
6990
// end replaceOneExample
91+
92+
// begin replaceoptions
93+
ReplaceOptions options = ReplaceOptions.sort(ascending("qty"));
94+
UpdateResult result = collection.replaceOne(filter, document, options);
95+
// end replaceoptions
7096
}
7197

7298
private void preview(boolean drop){

source/whats-new.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ and features:
4545
To learn about how to use this type when using the Atlas
4646
Vector Search feature, see the TODO add link :ref:`` guide.
4747

48+
.. replacement:: update-replace-example-link
49+
50+
the :ref:`java-fundamentals-change-document` guide
51+
4852
- Adds a client bulk write API that allows you to perform write operations on multiple
4953
databases and collections at once. To learn more about this feature, see the
5054
:ref:`java-sync-client-bulk-write` section of the Bulk Operations guide.

0 commit comments

Comments
 (0)