BSON_
On this page
Checks for the existence of data within a BSON document that matches specified filter conditions.
Syntax
Longhand Syntax
- 
          The BSON_function returns 1 if there exists a value in the BSON at the filter path for which the filter predicate evaluates to true.MATCH_ ANY BSON_MATCH_ANY(<filter_predicate>, <bson>, <filter_path>)
- 
          The BSON_function returns true if there is a value (possibly null) in the BSON at the filter path.MATCH_ ANY_ EXISTS BSON_MATCH_ANY_EXISTS(<bson>, <filter_path>)
Shorthand Syntax
- 
          filter_is the same as in the longhand syntax.predicate 
- 
          filter_is a dot-separated list of keys (as opposed to a comma-separated list of arguments in the longhand syntax).path 
- 
          ::?indicates the following period-separated values are array/object neutral.For example, each key can either be a direct field of a BSON object or a field of a BSON object within an array. 
- 
          EXISTS: As the optional<filter_is omitted in thepredicate> EXISTSfunction, the syntax and behavior of theEXISTSfunction is identical whether using either the shorthand or longhand syntax.BSON_MATCH_ANY(<bson>::?<filter_path>[, <filter_predicate>])
Arguments
- 
          <bson>: the name of a BSON column or a BSON value.
- 
          filter_: a path specification.path - 
              Longhand syntax: a comma-separated list of dictionary keys or zero-indexed array positions. 
- 
              Shorthand syntax: a period-separated list of dictionary keys or zero-indexed array positions. 
 
- 
              
- 
          filter_: an optional predicate expression which is applied to every value matching the filter path.predicate Consists of special built-in functions. 
Filter Path
- 
          The filter_, which is a set of keys or zero-indexed array positions, is used to recursively extract a BSON value or an element of a BSON array from thepath bsonargument.
- 
          If no filter_is provided, true is returned if the filter path exists.predicate 
- 
          The filter_is evaluated recursively against the BSON in thepath bsoncolumn using the following rules.- 
              If the current element identified by the filter_is an object, the next key in thepath filter_is matched against the keys in the object.path 
- 
              If the current element identified by the filter_is an array, the next key in thepath filter_is either matched as a zero-indexed position in the array or against the keys of any object that is an element of the array.path If both a zero-indexed array match or a key match are possible, the zero-indexed array match will be used. 
- 
              It is possible that there are several values in a bsonvalue which match thefilter_.path In this case, the filter_is applied on all values.predicate The function returns true if the predicate evalutates to true on one of the values. 
 
- 
              
Filter Predicate
- 
          The filter_is an optional predicate expression which is applied to every value matching thepredicate filter_.path 
- 
          Several special built-in functions are used for the filter_.predicate The built-in functions that can be used in the filter_include:predicate - 
              BSON_EXTRACT_ <type> 
- 
              BSON_NORMALIZE() 
- 
              MATCH_functions, which behave similar to the correspondingPARAM_ <type>_ STRICT() BSON_function.EXTRACT_ <type> Refer to BSON Functions in addition to the descriptions below for more information. 
- 
              MATCH_is used for all valid BSON types including true, false, BSON objects, and BSON arrays.PARAM_ BSON() There is intentionally no STRICT here. 
 
- 
              
- 
          The filter_is evaluated by running the specified built-in function on the value identified by thepredicate filter_.path If the value at the filter_does not match thepath <type>, the built-in evaluates as SQLNULL.
- 
          MATCH_will evaluate as a SQLPARAM_ <type>_ STRICT() NULLif there is not a match to<type>as defined below:- 
              MATCH_matches only numeric BSON values (integers/floating point).PARAM_ BIGINT_ STRICT() Rounds to the nearest integer. 
- 
              MATCH_matches only BSON booleans (true/false).PARAM_ BOOL_ STRICT() 
- 
              MATCH_matches all numeric types (integers/floats).PARAM_ DOUBLE_ STRICT() Unlike BSON_, it will not convert non-numeric types (booleans, strings, BSON objects, and BSON arrays) to double values.EXTRACT_ DOUBLE() 
- 
              MATCH_matches only BSON strings.PARAM_ STRING_ STRICT() Unlike BSON_it will not convert non-string types (not enclosed by quotes).EXTRACT_ STRING() Collation is controlled by the json_global variable.extract_ string_ collation 
- 
              MATCH_matches only BSON datetime types.PARAM_ DATETIME_ STRICT() 
- 
              MATCH_matches only BSON binary types.PARAM_ BINARY_ STRICT() 
 
- 
              
- 
          MATCH_when added to theELEMENTS BSON_function, matches only the array elements themselves, not the entire array.MATCH_ ANY 
Note
Refer to JSON_MATCH_ and examples of the shorthand syntax.
Return Type
- 
        Return type is TINYINT.
- 
        Returns 1 or 0 to indicate true or false. 
Return Value
- 
        If a filter_is provided, returns 1 if the predicate evaulates to true for any value found at thepredicate filter_.path 
- 
        If a filter_is not provided, returns 1 if thepredicate filter_exists in the BSON.path 
- 
        Returns 0otherwise.
Examples
Refer to JSON_
Usage of filter_ is shown in the following two queries, both of which return 1:
SELECT BSON_MATCH_ANY(MATCH_PARAM_BIGINT_STRICT() = 10, '[{"a":10}]':>BSON, '0', 'a') AS Result;
+--------+
| Result |
+--------+
|      1 |
+--------+SELECT BSON_MATCH_ANY(MATCH_PARAM_BIGINT_STRICT() = 10, '[{"a":10}]':>BSON, 'a') AS Result;
+--------+
| Result |
+--------+
|      1 |
+--------+The following examples in this section use this BSON object:
SELECT '{"a": [{ "x": "abc","y": {"key1": true,"key2": false }},{ "m": "cde","n": 234 },{ "p": true },{ "q": null }],"b": true}':>BSON INTO @bsonObj;
Here are some examples:
SELECT BSON_MATCH_ANY(MATCH_PARAM_BIGINT_STRICT() = 234,@bsonObj,'a','n') AS Result;
+--------+
| Result |
+--------+
|      1 |
+--------+SELECT BSON_MATCH_ANY(MATCH_PARAM_STRING_STRICT() = "abc",@bsonObj,'a','x') AS Result;
+--------+
| Result |
+--------+
|      1 |
+--------+SELECT BSON_MATCH_ANY_EXISTS(@bsonObj, 'a', 'y', 'key2') AS Result;
+--------+
| Result |
+--------+
|      1 |
+--------+Last modified: November 11, 2024