> This page location: /docs.md > /docs/drivers.md > /docs/languages/java.md > /docs/drivers/java/sync/current.md > /docs/drivers/java/sync/current/builders.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Updates Builders

## Overview

In this guide, you can learn how to specify **updates** by using [builders](https://www.mongodb.com/docs/drivers/java/sync/current/builders.md#std-label-java-builders) in the Java driver.

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

- [Field Updates](https://www.mongodb.com/docs/drivers/java/sync/current/builders/updates.md#std-label-field_updates): Updating or removing the value of a particular field

- [Array Updates](https://www.mongodb.com/docs/drivers/java/sync/current/builders/updates.md#std-label-array_updates): Updating values in an array-valued field

- [Combining Multiple Update Operators](https://www.mongodb.com/docs/drivers/java/sync/current/builders/updates.md#std-label-multiple_updates): 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](https://www.mongodb.com/docs/drivers/java/sync/current/data-formats/document-data-format-bson.md#std-label-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](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html) class statically:

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

The following examples assume this static import.

The examples in this guide use the following document:

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

## Field Updates

### Set

Use the [set()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#set\(java.lang.String,TItem\)) method to assign the value of a field in an update operation.

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

```java
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:

```json
{
   "_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:

```java
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:

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

### Unset

Use the [unset()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#unset\(java.lang.String\)) method to delete the value of a field in an update operation.

The following example deletes the `qty` field:

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

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

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

### Set On Insert

Use the [setOnInsert()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#setOnInsert\(java.lang.String,TItem\)) 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:

```java
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:

```json
{
   "_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.

### Increment

Use the [inc()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#inc\(java.lang.String,java.lang.Number\)) 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":

```java
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:

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

### Multiply

Use the [mul()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#mul\(java.lang.String,java.lang.Number\)) 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":

```java
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:

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

### Rename

Use the [rename()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#rename\(java.lang.String,java.lang.String\)) method to rename the value of a field in an update operation.

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

```java
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:

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

### Min

Use the [min()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#min\(java.lang.String,TItem\)) method to update the value of a field with the smaller number of the two specified in an update operation.

```java
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:

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

### Max

Use the [max()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#max\(java.lang.String,TItem\)) 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":

```java
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:

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

### Current Date

Use the [currentDate()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#currentDate\(java.lang.String\)) method to assign the value of a field in an update operation to the current date as a [BSON date.](https://www.mongodb.com/docs/manual/reference/bson-types/#date)

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

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

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

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

### Current Timestamp

Use the [currentTimestamp()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#currentTimestamp\(java.lang.String\)) method to assign the value of a field in an update operation to the current date as a [timestamp.](https://www.mongodb.com/docs/manual/reference/bson-types/#timestamps)

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

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

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

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

### Bit

Use the [bitwiseOr()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#bitwiseOr\(java.lang.String,int\)), [bitwiseAnd()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#bitwiseAnd\(java.lang.String,int\)), and [bitwiseXor()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#bitwiseXor\(java.lang.String,int\)) 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:

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

The bitwise operation results in 15:

```none
0101
1010
----
1111
```

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

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

## Array Updates

### Add to Set

Use the [addToSet()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#addToSet\(java.lang.String,TItem\)) 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:

```java
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:

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

### Pop

Use the [popFirst()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#popFirst\(java.lang.String\)) method to remove the first element of an array and the [popLast()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#popLast\(java.lang.String\)) 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:

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

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

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

### Pull All

Use the [pullAll()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#pullAll\(java.lang.String,java.util.List\)) 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:

```java
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:

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

### Pull

Use the [pull()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#pull\(java.lang.String,TItem\)) 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:

```java
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:

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

### Push

Use the [push()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Updates.html#push\(java.lang.String,TItem\)) method to append a value to an array in an update operation.

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

```java
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:

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

## Combining Multiple Update Operators

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:

```java
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:

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