BSON_BUILD_OBJECT

Creates a BSON object from the specified <key, value> pairs.

Syntax

BSON_BUILD_OBJECT([key1,value1 [,...]]])

Arguments

  • key: A valid utf8mb4 string that represents a key. It must not contain \0 characters in the middle of the string.

  • value: A valid BSON value or an expression that evaluates to a valid BSON value.

Return Type

A BSON object.

Remarks

  • If the key and value arguments are not specified, returns an empty BSON object.

  • If the specified value is a SQL NULL, the corresponding <key, value> pair is omitted.

  • If only SQL NULL values are specified in the value argument, an empty BSON object is returned.

  • Returns an error if an odd number of arguments are specified.

  • If the input arguments include identical keys, the resulting object has the value corresponding to the last merged value for each respective key.

Examples

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

SELECT BSON_BUILD_OBJECT('field1_integer', 54:>BSON, 'field2_boolean', 'true':>BSON, 'field3_string', '"a BSON string"':>BSON, 'field4_array', '[0,1,2]':>BSON):>JSON AS Result;
+----------------------------------------------------------------------------------------------------+
| Result                                                                                             |
+----------------------------------------------------------------------------------------------------+
| {"field1_integer":54,"field2_boolean":true,"field3_string":"a BSON string","field4_array":[0,1,2]} |
+----------------------------------------------------------------------------------------------------+
SELECT BSON_BUILD_OBJECT('field1_datetime', '2023-02-06 19:05:37.234':>DATETIME(6):>BSON, 'field2_document', '{"a":123}':>BSON, 'field3_null', 'null':>BSON):>JSON AS Result;
+---------------------------------------------------------------------------------------------------------+
| Result                                                                                                  |
+---------------------------------------------------------------------------------------------------------+
| {"field1_datetime":{"$date":"2023-02-06T19:05:37.234Z"},"field2_document":{"a":123},"field3_null":null} |
+---------------------------------------------------------------------------------------------------------+
SELECT BSON_BUILD_OBJECT():>JSON;
+--------+
| Result |
+--------+
| {}     |
+--------+

In the following example, only the last key/value pair for the key 'field_duplicate' is added to the BSON object because the input argument has multiple keys named 'field_duplicate'.

SELECT BSON_BUILD_OBJECT('field_duplicate', 87:>BSON, 'field2', '"text"':>BSON, 'field_duplicate', '{"a":40}':>BSON):>JSON AS Result;
+----------------------------------------------+
| Result                                       |
+----------------------------------------------+
| {"field2":"text","field_duplicate":{"a":40}} |
+----------------------------------------------+

In the following example, keys with SQL NULL values are ignored and BSON NULL values are added to the BSON object.

SELECT BSON_BUILD_OBJECT("field1", NULL, "field2", NULL, "field3", null):>JSON AS Result;
+--------+
| Result |
+--------+
| {}     |
+--------+
SELECT BSON_BUILD_OBJECT("field1", NULL, "field2", NULL, "field3", 'null':>BSON):>JSON AS Result;
+-----------------+
| Result          |
+-----------------+
| {"field3":null} |
+-----------------+

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