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), 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

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

    SELECT BSON_UPDATE('{"a":100}':>BSON, '{"a":[{"$inc":50}]}':>BSON):>JSON AS Result;
    +-----------+
    | Result    |
    +-----------+
    | {"a":150} |
    +-----------+
  • Add a new field

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

    SELECT BSON_UPDATE('{"a":100, "b":50}':>BSON, '{"b":[{"$unset":1}]}':>BSON):>JSON AS Result;
    +-----------+
    | Result    |
    +-----------+
    | {"a":100} |
    +-----------+
  • Add elements to an array

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

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

Last modified: July 10, 2025

Was this article helpful?

Verification instructions

Note: You must install cosign to verify the authenticity of the SingleStore file.

Use the following steps to verify the authenticity of singlestoredb-server, singlestoredb-toolbox, singlestoredb-studio, and singlestore-client SingleStore files that have been downloaded.

You may perform the following steps on any computer that can run cosign, such as the main deployment host of the cluster.

  1. (Optional) Run the following command to view the associated signature files.

    curl undefined
  2. Download the signature file from the SingleStore release server.

    • Option 1: Click the Download Signature button next to the SingleStore file.

    • Option 2: Copy and paste the following URL into the address bar of your browser and save the signature file.

    • Option 3: Run the following command to download the signature file.

      curl -O undefined
  3. After the signature file has been downloaded, run the following command to verify the authenticity of the SingleStore file.

    echo -n undefined |
    cosign verify-blob --certificate-oidc-issuer https://oidc.eks.us-east-1.amazonaws.com/id/CCDCDBA1379A5596AB5B2E46DCA385BC \
    --certificate-identity https://kubernetes.io/namespaces/freya-production/serviceaccounts/job-worker \
    --bundle undefined \
    --new-bundle-format -
    Verified OK