- Builders
- Updates
Updates
The Updates
class provides static factory methods for all the MongoDB update
operators. Each method returns an instance of the Bson
type, which can in turn be passed to
any method that expects an update.
For brevity, you may choose to import the methods of the Updates
class statically:
import static com.mongodb.client.model.Updates.*;
All the examples below assume this static import.
Field Updates
This section describes update operators that apply to the value of an entire field.
Set
The $set
update operator sets the value of a field to to the specified value.
This example sets the value of the quantity
field to 11
:
set("quantity", 11)
Unset
The $unset
update operator deletes the field with the given name.
This example deletes the quantity
field:
unset("quantity")
Set On Insert
The $setOnInsert
update operator sets the value of a field to the given
value, but only if the update is an
upsert that results in an
insert of a document.
This example sets the value of the defaultQuantity
field to 10
if an upsert resulted in the insert of a document:
setOnInsert("defaultQuantity", 10)
Increment
The $inc
update operator increments the value of a numeric field by a specified
value.
This example increments the value of the quantity
field by 5
:
inc("quantity", 5)
Multiply
The $mul
update operator multiplies the value of a numeric field by a specified value.
This example multiplies the value of the price
field by 1.2
:
mul("price", 1.2)
Rename
The $rename
update operator renames a field.
This example renames the qty
field to quantity
:
rename("qty", "quantity")
Min
The $min
update operator updates the value of the field to a specified value if the
specified value is less than the current value of the field .
This example sets the value of the lowScore
field to the minimum of its current value and 150:
min("lowScore", 150)
Max
The $max
update operator updates the value of the field to a specified value if
the specified value is greater than the current value of the field .
This example sets the value of the highScore
field to the maximum of its current value and 900:
max("highScore", 900)
Current Date
The $currentDate
update operator sets the value of the field with the
specified name to the current date, either as a BSON date or as a BSON
timestamp.
This example sets the value of the lastModified
field to the current date as a BSON date type:
currentDate("lastModified")
This example sets the value of the lastModified
field to the current date as a BSON timestamp type:
currentTimestamp("lastModified")
Bit
The $bit
update operator performs a bitwise update of the integral value of a field.
This example performs a bitwise AND between the number 10
and the integral value of the mask
field:
bitwiseAnd("mask", 10)
This example performs a bitwise OR between the number 10
and the integral value of the mask
field:
bitwiseOr("mask", 10)
This example performs a bitwise XOR between the number 10
and the integral value of the mask
field:
bitwiseXor("mask", 10)
Array Updates
This section describes update operators that apply to the contents of the array value of a field.
Add to Set
The $addToSet
update operator adds a value to an array unless the value is
already present, in which case $addToSet does nothing to that array.
This example adds the value "a"
to the array value of the `letters’ field:
addToSet("letters", "a")
This example adds each of the values "a"
, "b"
, and "c"
to the array value of the `letters’ field:
addEachToSet("letters", Arrays.asList("a", "b", "c"))
Pop
The $pop
update operator removes the first or last element of an array.
This example pops the first element off of the array value of the scores
field:
popFirst("scores")
This example pops the last element off of the array value of the scores
field:
popLast("scores")
Pull All
The $pullAll
update operator removes all instances of the specified values from
an existing array.
This example removes the scores 0
and 5
from the scores
array:
pullAll("scores", Arrays.asList(0, 5))
Pull
The $pull
update operator removes from an existing array all instances of a value
or values that match a specified query.
This example removes the value 0
from the scores
array:
pull("scores", 0)
This example removes all elements from the votes
array that are greater than or equal to 6
:
pullByFilter(Filters.gte("votes", 6))
Push
The $push
update operator appends a specified value to an array.
This examples pushes the value 89
to the scores
array:
push("scores", 89)
This examples pushes each of the values 89
, 90
, and 92
to the scores
array:
pushEach("scores", Arrays.asList(89, 90, 92))
This example pushes each of the values 89
, 90
, and 92
to the start of the scores
array:
pushEach("scores", Arrays.asList(89, 90, 92), new PushOptions().position(0))
This example pushes each of the values 89
, 90
, and 92
to the scores
array, sorts the array in descending order, and removes all
but the first 5 elements of the array:
pushEach("scores", Arrays.asList(89, 90, 92), new PushOptions().sort(-1).slice(5))
This example pushes each of the documents { wk: 5, score: 8 }
, { wk: 6, score: 7 }
, and { wk: 7, score: 6 }
to the quizzes
array,
sorts the array in descending order by score
, and removes all but the last 3 elements of the array:
pushEach("quizzes",
Arrays.asList(new Document("week", 5).append("score", 8),
new Document("week", 6).append("score", 7),
new Document("week", 7).append("score", 6)),
new PushOptions().sortDocument(Sorts.descending("score")).slice(-3))
Combining Multiple Update Operators
Often, an application will need to atomically update multiple fields of a single document by combine two or more of the update operators described above.
This example sets the value of the quantity
field to 11, the value of the total
field to 30.40
, and pushes each of the values
4.99
, 5.99
, and 10.99
to the array value of the prices
field:
combine(set("quantity", 11),
set("total", 30.40),
pushEach("prices", Arrays.asList(4.99, 5.99, 10.99)))