Docs Menu
Docs Home
/ / /
Java Sync Driver
/ /

Updates Builders

On this page

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

In this guide, you can learn how to specify updates by using builders in the Java driver.

The Updates builder provides helper methods to simplify the following tasks:

  • Field Updates: Updating or removing the value of a particular field

  • Array Updates: Updating values in an array-valued field

  • Combining Multiple Update Operators: Performing multiple updates at once, such as setting or changing more than one field

Some methods that expect update documents are:

  • updateOne()

  • updateMany()

  • bulkWrite()

The Updates class provides static factory methods for all the MongoDB update operators. Each method returns an instance of the BSON type, which you can pass to any method that expects an update argument.

Tip

For brevity, you can choose to import the methods of the Updates class statically:

import static com.mongodb.client.model.Updates.*;

The following examples assume this static import.

The examples in this guide use the following document:

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Use the set() method to assign the value of a field in an update operation.

The following example sets the value of the qty field to "11":

Bson filter = eq("_id", 1);
Bson update = set("qty", 11);
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 11,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

The following example adds two new fields to the original document:

Bson filter = eq("_id", 1);
Bson update = combine(set("width", 6.5), set("height", 10));
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" },
"width": 6.5,
"height": 10,
}

Use the unset() method to delete the value of a field in an update operation.

The following example deletes the qty field:

Bson filter = eq("_id", 1);
Bson update = unset("qty");
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Use the setOnInsert() method to assign the value of a field in an update operation on an insert of a document.

The following example sets the value of the qty field to "5" if an upsert resulted in the insert of a document:

Bson filter = eq("_id", 1);
Bson update = setOnInsert("qty", 7);
collection.updateOne(filter, update, new UpdateOptions().upsert(true));

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Note

If the document is not inserted, no change will occur.

Use the inc() method to increment the value of a numeric field in an update operation.

The following example increments the value of the qty field by "3":

Bson filter = eq("_id", 1);
Bson update = inc("qty", 3);
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 8,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Use the mul() method to multiply the value of a numeric field in an update operation.

The following example multiplies the value of the qty field by "2":

Bson filter = eq("_id", 1);
Bson update = mul("qty", 2);
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 10,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Use the rename() method to rename the value of a field in an update operation.

The following example renames the qty field to "quantity":

Bson filter = eq("_id", 1);
Bson update = rename("qty", "quantity");
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" },
"quantity": 5
}

Use the min() method to update the value of a field with the smaller number of the two specified in an update operation.

Bson filter = eq("_id", 1);
Bson update = min("qty", 2);
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 2,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Use the max() method to update the value of a field with the larger number of the two specified in an update operation.

The following example sets the value of the qty field to the maximum of its current value and "8":

Bson filter = eq("_id", 1);
Bson update = max("qty", 8);
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 8,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Use the currentDate() method to assign the value of a field in an update operation to the current date as a BSON date.

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

Bson filter = eq("_id", 1);
Bson update = currentDate("lastModified");
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-22T21:01:20.027Z" }
}

Use the currentTimestamp() method to assign the value of a field in an update operation to the current date as a timestamp.

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

Bson filter = eq("_id", 1);
Bson update = currentTimestamp("lastModified");
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$timestamp": { "t": 1616446880, "i": 5 } }
}

Use the bitwiseOr(), bitwiseAnd(), and bitwiseXor() methods to perform a bitwise update of the integer value of a field in an update operation.

The following example performs a bitwise AND between the number "10" and the integer value of the qty field:

Bson filter = eq("_id", 1);
Bson update = bitwiseOr("qty", 10);
collection.updateOne(filter, update);

The bitwise operation results in 15:

0101
1010
----
1111

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 15,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Use the addToSet() method to append a value to an array if the value is not already present in an update operation.

The following example adds the value "C" to the array value of the vendor field:

Bson filter = eq("_id", 1);
Bson update = addToSet("vendor", "C");
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M", "C" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Use the popFirst() method to remove the first element of an array and the popLast() method to remove the last element of an array in an update operation.

The following example pops the first element off of the array value of the vendor field:

Bson filter = eq("_id", 1);
Bson update = popFirst("vendor");
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Use the pullAll() method to remove all instances of values from an existing array in an update operation.

The following example removes vendor "A" and "M" from the vendor array:

Bson filter = eq("_id", 1);
Bson update = pullAll("vendor", Arrays.asList("A", "M"));
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "D" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Use the pull() method to remove all instances of a value from an existing array in an update operation.

The following example removes the value "D" from the vendor array:

Bson filter = eq("_id", 1);
Bson update = pull("vendor", "D");
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Use the push() method to append a value to an array in an update operation.

The following examples pushes "C" to the vendor array:

Bson filter = eq("_id", 1);
Bson update = push("vendor", "C");
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M", "C" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

An application can update multiple fields of a single document by combining two or more of the update operators described in the preceding sections.

The following example increments the value of the qty field by "6", sets the value of the color field to "purple", and pushes "R" to the vendor field:

Bson filter = eq("_id", 1);
Bson update = combine(set("color", "purple"), inc("qty", 6), push("vendor", "R"));
collection.updateOne(filter, update);

The preceding example updates the original document to the following state:

{
"_id": 1,
"color": "purple",
"qty": 11,
"vendor": [ "A", "D", "M", "R" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

Back

Sort