# BSON\_UPDATE

Updates a BSON document.

Accepts two BSON documents, updates the first document using the specified update operators in the second document (similar to the MongoDB® [update operators](https://www.mongodb.com/docs/v7.0/reference/operator/update/)), and returns the updated document.

## Syntax

```
BSON_UPDATE(<bson_to_update>, <update>)
```

## Arguments

* `bson_to_update`: A valid BSON document or an expression that evaluates to a valid BSON document to update.
* `update`: A valid BSON document that specifies the update operation to perform.

## Return Type

A `BSON` value.

## Remarks

`BSON_UPDATE()` does not support the following update operators:

* `$pull`
* `$rename`
* `$setOnInsert`

## Examples

**Note**: The following examples explicitly cast string to BSON for clarity. Similarly, the output is cast to JSON.

The following are examples of different update operations on a BSON document or array:

* Multiply
  ```sql
  SELECT BSON_UPDATE('{"a":100, "b":50}':>BSON, '{"a":[{"$mul":2}]}':>BSON):>JSON AS Result;

  ```
  ```output

  +------------------+
  | Result           |
  +------------------+
  | {"a":200,"b":50} |
  +------------------+
  ```
* Increment
  ```sql
  SELECT BSON_UPDATE('{"a":100}':>BSON, '{"a":[{"$inc":50}]}':>BSON):>JSON AS Result;

  ```
  ```output

  +-----------+
  | Result    |
  +-----------+
  | {"a":150} |
  +-----------+
  ```
* Add a new field
  ```sql
  SELECT BSON_UPDATE('{"a":100, "b":50}':>BSON, '{"c":[{"$set":"New field"}]}':>BSON):>JSON AS Result;

  ```
  ```output

  +----------------------------------+
  | Result                           |
  +----------------------------------+
  | {"a":100,"b":50,"c":"New field"} |
  +----------------------------------+
  ```
* Remove a field
  ```sql
  SELECT BSON_UPDATE('{"a":100, "b":50}':>BSON, '{"b":[{"$unset":1}]}':>BSON):>JSON AS Result;

  ```
  ```output

  +-----------+
  | Result    |
  +-----------+
  | {"a":100} |
  +-----------+
  ```
* Add elements to an array
  ```sql
  SELECT BSON_UPDATE('{"a":[1,2,3]}':>BSON, '{"a":[{"$addToSet":{"$each":[4,5,4]}}]}':>BSON):> JSON AS Result;

  ```
  ```output

  +-------------------+
  | Result            |
  +-------------------+
  | {"a":[1,2,3,4,5]} |
  +-------------------+
  ```
  ```sql
  SELECT BSON_UPDATE('{"a":[1,2,3]}':>BSON, '{"a":[{"$push":{"$each":[4,5]}}]}':>BSON):>JSON AS Result;

  ```
  ```output

  +-------------------+
  | Result            |
  +-------------------+
  | {"a":[1,2,3,4,5]} |
  +-------------------+
  ```
* Remove elements from an array
  ```sql
  SELECT BSON_UPDATE('{"a":[1,2,3,4,5]}':>BSON, '{"a":[{"$pop":1}]}':>BSON):> JSON AS Result;

  ```
  ```output

  +-----------------+
  | Result          |
  +-----------------+
  | {"a":[1,2,3,4]} |
  +-----------------+
  ```
  ```sql
  SELECT BSON_UPDATE('{"a":[1,1,2,2,3,4,4,5]}':>BSON, '{"a":[{"$pullAll":[1,4]}]}':>BSON):> JSON AS Result;

  ```
  ```output

  +-----------------+
  | Result          |
  +-----------------+
  | {"a":[2,2,3,5]} |
  +-----------------+
  ```

***

Modified at: July 10, 2025

Source: [/cloud/reference/sql-reference/bson-functions/bson-update/](https://docs.singlestore.com/cloud/reference/sql-reference/bson-functions/bson-update/)

(An index of the documentation is available at /llms.txt)
