Docs Menu
Docs Home
/ / /
Scala
/

Updates

On this page

  • Field Updates
  • Set
  • Unset
  • Set On Insert
  • Increment
  • Multiply
  • Rename
  • Min
  • Max
  • Current Date
  • Bit
  • Array Updates
  • Add to Set
  • Pop
  • Pull All
  • Pull
  • Push
  • Combining Multiple Update Operators

The Updates class provides static factory methods for 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.

You can import the methods of the Updates class statically, as shown in the following code:

import org.mongodb.scala.model.Updates._

The examples in this guide assume this static import.

This section describes update operators that apply to the value of an entire field.

The $set update operator sets the value of a field to the specified value.

The following example sets the value of the quantity field to 11:

set("quantity", 11)

The $unset update operator deletes the field with the given name.

The following example deletes the quantity field:

unset("quantity")

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.

The following example sets the value of the defaultQuantity field to 10 if an upsert resulted in the insert of a document:

setOnInsert("defaultQuantity", 10)

The $inc update operator increments the value of a numeric field by a specified value.

The following example increments the value of the quantity field by 5:

inc("quantity", 5)

The $mul update operator multiplies the value of a numeric field by a specified value.

The following example multiplies the value of the price field by 1.2:

mul("price", 1.2)

The $rename update operator renames a field.

The following example renames the qty field to quantity:

rename("qty", "quantity")

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.

The following example sets the value of the lowScore field to the minimum of its current value and 150:

min("lowScore", 150)

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.

The following example sets the value of the highScore field to the maximum of its current value and 900:

max("highScore", 900)

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.

The following example sets the value of the lastModified field to the current date as a BSON date type:

currentDate("lastModified")

The following example sets the value of the lastModified field to the current date as a BSON timestamp type:

currentTimestamp("lastModified")

The $bit update operator performs a bitwise update of the integral value of a field.

The following example performs a bitwise AND between the number 10 and the integral value of the mask field:

bitwiseAnd("mask", 10)

The following example performs a bitwise OR between the number 10 and the integral value of the mask field:

bitwiseOr("mask", 10)

The following example performs a bitwise XOR between the number 10 and the integral value of the mask field:

bitwiseXor("mask", 10)

This section describes update operators that apply to the contents of the array value of a field.

The $addToSet update operator adds a value to an array unless the value is already present, in which case the operator does nothing to that array.

The following example adds the value "a" to the array value of the letters field:

addToSet("letters", "a")

The following 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"))

The $pop update operator removes the first or last element of an array.

The following example removes the first element of the array value of the scores field:

popFirst("scores")

The following example removes the last element of the array value of the scores field:

popLast("scores")

The $pullAll update operator removes all instances of the specified values from an existing array.

The following example removes the scores 0 and 5 from the scores array:

pullAll("scores", Arrays.asList(0, 5))

The $pull update operator removes from an existing array all instances of a value or values that match a specified query.

The following example removes the value 0 from the scores array:

pull("scores", 0)

The following example removes all elements from the votes array that are greater than or equal to 6:

pullByFilter(Filters.gte("votes", 6))

The $push update operator appends a specified value to an array.

The following example pushes the value 89 to the scores array:

push("scores", 89)

The following example pushes the values 89, 90, and 92 to the scores array:

pushEach("scores", 89, 90, 92)

The following example pushes the values 89, 90, and 92 to the start of the scores array:

pushEach("scores", new PushOptions().position(0), 89, 90, 92)

The following example pushes 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", new PushOptions().sort(-1).slice(5), 89, 90, 92)

The following example pushes 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", new PushOptions().sortDocument(Sorts.descending("score")).slice(-3),
Document("week" -> 5, "score" -> 8),
Document("week" -> 6, "score" -> 7),
Document("week" -> 7, "score" -> 6))

Often, an application must atomically update multiple fields of a single document by combining two or more of the update operators described in the preceding sections.

The following example sets the value of the quantity field to 11, the value of the total field to 30.40, and pushes 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", 4.99, 5.99, 10.99))

Back

Aggregates