Skip to content

Commit d177f66

Browse files
committed
DOCSP-35926: surface combine information (#542)
* DOCSP-35926: surface combine information * fix (cherry picked from commit 5432fbb)
1 parent a7131ed commit d177f66

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

source/fundamentals/builders/updates.txt

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Updates Builders
33
================
44

5-
.. default-domain:: mongodb
6-
75
.. contents:: On this page
86
:local:
97
:backlinks: none
@@ -15,16 +13,17 @@ Updates Builders
1513
Overview
1614
--------
1715

18-
In this guide, you can learn how to specify **updates** using
19-
:doc:`builders </fundamentals/builders/>` in the MongoDB Java driver.
16+
In this guide, you can learn how to specify **updates** by using
17+
:ref:`builders <java-builders>` in the {+driver-short+}.
2018

21-
The ``Updates`` builder provides helper methods for the following types of updates:
19+
The ``Updates`` builder provides helper methods to simplify the following
20+
tasks:
2221

23-
- :ref:`Field Updates <field_updates>`
24-
- :ref:`Array Updates <array_updates>`
25-
- :ref:`Combining Multiple Update Operators <multiple_updates>`
22+
- :ref:`field_updates`: Updating or removing the value of a particular field
23+
- :ref:`array_updates`: Updating values in an array-valued field
24+
- :ref:`multiple_updates`: Performing multiple updates at once, such as setting or changing more than one field
2625

27-
Some methods that expect updates are:
26+
Some methods that expect update documents are:
2827

2928
- ``updateOne()``
3029
- ``updateMany()``
@@ -36,7 +35,9 @@ type, which you can pass to any method that expects an update argument.
3635

3736
.. tip::
3837

39-
For brevity, you may choose to import the methods of the `Updates <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Updates.html>`__ class statically:
38+
For brevity, you can choose to import the methods of the `Updates
39+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Updates.html>`__
40+
class statically:
4041

4142
.. code-block:: java
4243

@@ -88,6 +89,29 @@ The preceding example updates the original document to the following state:
8889
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
8990
}
9091

92+
The following example adds two new fields to the original document:
93+
94+
.. literalinclude:: /includes/fundamentals/code-snippets/builders/Updates.java
95+
:language: java
96+
:dedent:
97+
:start-after: begin combineSet
98+
:end-before: end combineSet
99+
100+
The preceding example updates the original document to the following state:
101+
102+
.. code-block:: json
103+
:copyable: false
104+
105+
{
106+
"_id": 1,
107+
"color": "red",
108+
"qty": 5,
109+
"vendor": [ "A", "D", "M" ],
110+
"lastModified": { "$date": "2021-03-05T05:00:00Z" },
111+
"width": 6.5,
112+
"height": 10,
113+
}
114+
91115
Unset
92116
~~~~~
93117
Use the `unset() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Updates.html#unset(java.lang.String)>`__ method

source/includes/fundamentals/code-snippets/builders/Updates.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public static void main(String[] args) {
4343
updates.setUpdate();
4444
updates.resetCollection(updates);
4545

46+
System.out.println("combineSet:");
47+
updates.combineSet();
48+
updates.resetCollection(updates);
49+
4650
System.out.println("unsetUpdate:");
4751
updates.unsetUpdate();
4852
updates.resetCollection(updates);
@@ -116,6 +120,15 @@ private void setUpdate() {
116120
collection.updateOne(filter, update);
117121
// end setUpdate
118122
}
123+
124+
private void combineSet() {
125+
// begin combineSet
126+
Bson filter = eq("_id", 1);
127+
Bson update = combine(set("width", 6.5), set("height", 10));
128+
collection.updateOne(filter, update);
129+
// end combineSet
130+
}
131+
119132
private void unsetUpdate() {
120133
// begin unsetUpdate
121134
Bson filter = eq("_id", 1);

0 commit comments

Comments
 (0)