Skip to content

Commit f3aea8f

Browse files
authored
DOCSP-46124: sort option in updateoptions and replaceoptions (#609)
* DOCSP-46124: sort option for updateone and replaceone * add links * wip * wip * NR PR fixes 1 * add bulk info * JK tech review 1
1 parent 06bbd55 commit f3aea8f

File tree

5 files changed

+138
-28
lines changed

5 files changed

+138
-28
lines changed

source/fundamentals/builders/updates.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _updates-builders:
2+
13
================
24
Updates Builders
35
================
@@ -8,8 +10,6 @@ Updates Builders
810
:depth: 2
911
:class: singlecol
1012

11-
.. _updates-builders:
12-
1313
Overview
1414
--------
1515

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,25 @@ contains an added ``location`` field:
157157
:start-after: begin replaceDocumentsExample
158158
:end-before: end replaceDocumentsExample
159159

160+
If multiple documents match the query filter specified in
161+
the ``ReplaceOneModel`` instance, the operation replaces the first
162+
result. You can specify a sort in a ``ReplaceOptions`` instance to apply
163+
an order to matched documents before the driver performs the replace
164+
operation, as shown in the following code:
165+
166+
.. code-block:: java
167+
168+
ReplaceOptions options = ReplaceOptions.sort(Sorts.ascending("_id"));
169+
160170
For more information about the methods and classes mentioned in this section,
161171
see the following resources:
162172

163-
- `ReplaceOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/ReplaceOneModel.html>`__ API documentation
173+
- `ReplaceOneModel
174+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/ReplaceOneModel.html>`__
175+
API documentation
176+
- `ReplaceOptions
177+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/ReplaceOptions.html>`__
178+
API documentation
164179
- :manual:`Unique indexes </core/index-unique/>` Server Manual Explanation
165180

166181
Update Operation
@@ -193,11 +208,28 @@ the ``age`` field in a document where the ``_id`` is ``2``:
193208
:start-after: begin updateDocumentsExample
194209
:end-before: end updateDocumentsExample
195210

211+
If multiple documents match the query filter specified in
212+
the ``UpdateOneModel`` instance, the operation updates the first
213+
result. You can specify a sort in an ``UpdateOptions`` instance to apply
214+
an order to matched documents before the driver performs the update
215+
operation, as shown in the following code:
216+
217+
.. code-block:: java
218+
219+
UpdateOptions options = UpdateOptions.sort(Sorts.ascending("_id"));
220+
196221
For more information about the methods and classes mentioned in this section,
197222
see the following resources:
198223

199-
- `UpdateOneModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateOneModel.html>`__ API documentation
200-
- `UpdateManyModel <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateManyModel.html>`__ API documentation
224+
- `UpdateOneModel
225+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateOneModel.html>`__
226+
API documentation
227+
- `UpdateManyModel
228+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateManyModel.html>`__
229+
API documentation
230+
- `UpdateOptions
231+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateOptions.html>`__
232+
API documentation
201233
- :manual:`unique indexes </core/index-unique/>` Server Manual Explanation
202234

203235
Delete Operation

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

Lines changed: 70 additions & 23 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
@@ -56,40 +56,73 @@ You can call the ``updateOne()`` and ``updateMany()`` methods on a
5656

5757
.. code-block:: java
5858

59-
collection.updateOne(query, updateDocument);
60-
61-
collection.updateMany(query, updateDocument);
59+
collection.updateOne(<query>, <updateDocument>);
60+
collection.updateMany(<query>, <updateDocument>);
6261

6362
Update Operation Parameters
6463
~~~~~~~~~~~~~~~~~~~~~~~~~~~
6564

6665
The ``updateOne()`` and ``updateMany()`` methods both have the following
6766
parameters:
6867

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.
68+
- ``query`` specifies a query filter with the criteria to match
69+
documents to update in your collection.
70+
- ``update`` specifies the fields and values to modify in the matching
71+
document or documents. The examples in this section use the
72+
:ref:`updates-builders` to create the update document.
73+
- *(Optional)* ``updateOptions`` specifies options that you can set to
74+
customize how the driver performs the update operation. To learn more
75+
about this type, see the API documentation for `UpdateOptions
76+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/UpdateOptions.html>`__.
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 update = Updates.operator(<field>, <value>);
84+
85+
To view a complete list of Updates builders and their usage, see `Updates
86+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Updates.html>`__
87+
in the API documentation.
88+
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
78101

79-
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>`__.
102+
If multiple documents match the query filter specified in
103+
the ``updateOne()`` method, it updates 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 update operation, as
106+
shown in the following code:
81107

82-
Example
83-
```````
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+
- Query filter that matches all the colors
124+
- Update document that contains instructions to increment the ``qty``
125+
field by ``20``
93126

94127
.. literalinclude:: /includes/fundamentals/code-snippets/Update.java
95128
:language: java
@@ -149,18 +182,24 @@ instance as follows:
149182

150183
.. code-block:: java
151184

152-
collection.replaceOne(query, replacementDocument);
185+
collection.replaceOne(<query>, <replacement>);
153186

154187
Replace Operation Parameters
155188
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156189

157190
The ``replaceOne()`` method has the following parameters:
158191

159-
- ``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+
- ``query`` specifies a query filter with the criteria to match a
193+
document to replace in your collection.
194+
- ``replacement`` specifies fields and values of a new ``Document``
195+
object to replace the matched document.
196+
- *(Optional)* ``replaceOptions`` specifies options that you can set to
197+
customize how the driver performs the replace operation. To learn more
198+
about this type, see the API documentation for `ReplaceOptions
199+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/ReplaceOptions.html>`__.
161200

162-
Example
163-
```````
201+
Replace One Example
202+
~~~~~~~~~~~~~~~~~~~
164203

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

193232
{ "_id": 5, "color": "orange", "qty": 25 }
194233

234+
If multiple documents match the query filter specified in
235+
the ``replaceOne()`` method, it replaces the first result. You can
236+
specify a sort in a ``ReplaceOptions`` instance to apply an order to
237+
matched documents before the driver performs the replace operation, as
238+
shown in the following code:
239+
240+
.. literalinclude:: /includes/fundamentals/code-snippets/Update.java
241+
:language: java
242+
:dedent:
243+
:start-after: begin replaceoptions
244+
:end-before: end replaceoptions
245+
195246
If zero documents match the query filter in the replace operation,
196247
``replaceOne()`` makes no changes to documents in the collection. See
197248
our :doc:`upsert guide </fundamentals/crud/write-operations/upsert>` to
198249
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.
250+
documents match.
203251

204252
.. important::
205253

@@ -208,4 +256,3 @@ the ``replaceOne()`` method, it replaces the first result.
208256
For more information about constraints on unique indexes,
209257
see :manual:`Unique Indexes </core/index-unique/>` in the
210258
{+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 matching document
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ and features:
4545
To learn about how to use this type when using the Atlas
4646
Vector Search feature, see the :ref:`java-atlas-vector-search` guide.
4747

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

0 commit comments

Comments
 (0)