BSON_SET_BSON

Sets (or overwrites) the value at the specified key in a BSON object or array and returns the BSON object or array.

Syntax

BSON_SET_BSON(<input>, <key_path>, <bson_value>)

Arguments

  • <input>: A valid BSON object or array, or an expression that evaluates to a valid BSON object or array.

  • <key_path>: The path to the key in the BSON object or array.

  • <bson_value>: A valid BSON value or en expression that evaluates to a valid BSON value.

Return Type

A BSON object or array.

Remarks

  • If the specified key does not exist, it is added at the specified path in the BSON object.

  • If the path represents an index in the array that doesn't exist, the value is added at the specified index and the missing elements in the array leading to the specified index are added as BSON NULL.

  • If the <input> is not a BSON object or array, it is returned as is.

Examples

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

The following example updates the value of the key at the specified path.

SELECT BSON_SET_BSON('{"a":10,"b":true}':>BSON, 'b', '{"$numberDecimal":"125.55"}':>BSON):>JSON AS Result;
+------------------------------------------+
| Result                                   |
+------------------------------------------+
| {"a":10,"b":{"$numberDecimal":"125.55"}} |
+------------------------------------------+

The following example adds a key at the specified path because the specified path doesn't exist in the BSON object.

SELECT BSON_SET_BSON('{"a":10,"b":true}':>BSON, 'c', 'd', 'e', '{"$numberDecimal":"125.55"}':>BSON):>JSON AS Result;
+---------------------------------------------------------------+
| Result                                                        |
+---------------------------------------------------------------+
| {"a":10,"b":true,"c":{"d":{"e":{"$numberDecimal":"125.55"}}}} |
+---------------------------------------------------------------+

The following examples update a value in the array.

SELECT BSON_SET_BSON('{"a":10,"b":{"x":[0,1,2,3,4]}}':>BSON, 'b', 'x', '0', '"new_val"':>BSON):>JSON AS Result;
+----------------------------------------+
| Result                                 |
+----------------------------------------+
| {"a":10,"b":{"x":["new_val",1,2,3,4]}} |
+----------------------------------------+
SELECT BSON_SET_BSON('{"a":10,"b":{"x":[0,1,2,3,4]}}':>BSON, 'b', 'x', '0','a', '1':>BSON):>JSON AS Result;
+--------------------------------------+
| Result                               |
+--------------------------------------+
| {"a":10,"b":{"x":[{"a":1},1,2,3,4]}} |
+--------------------------------------+

The following example adds a value at the specified index in the array. The missing indexes are added with NULL values.

SELECT BSON_SET_BSON('{"a":10,"b":{"x":[0,1,2,3,4],"f":"string value"}}':>BSON, 'b', 'x', '8', '8':>BSON):>JSON AS Result;
+--------------------------------------------------------------------+
| Result                                                             |
+--------------------------------------------------------------------+
| {"a":10,"b":{"f":"string value","x":[0,1,2,3,4,null,null,null,8]}} |
+--------------------------------------------------------------------+

Last modified: May 8, 2024

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