BSON_ MATCH_ ANY and BSON_ MATCH_ ANY_ EXISTS
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> EXISTS
function, the syntax and behavior of theEXISTS
function 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 bson
argument. -
If no
filter_
is provided, true is returned if the filter path exists.predicate -
The
filter_
is evaluated recursively against the BSON in thepath bson
column 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
bson
value 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() NULL
if 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 0 otherwise.
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