BSON_ MATCH_ ANY and BSON_ MATCH_ ANY_ EXISTS
On this page
BSON_
returns 1
if any BSON value at the specified filter path matches the filter predicate in a BSON object.BSON_
returns 1
if the specified filter path exists in the BSON object.
Syntax
BSON_MATCH_ANY(<filter_predicate>, <bson>, <filter_path>)
BSON_MATCH_ANY_EXISTS(<bson>,<filter_path>)
Arguments
-
<bson>
: A valid BSON object or an expression that evaluates to a valid BSON object. -
<filter_
: A comma separated list of keys or array positions (zero-based index) that represents the path.path> -
<filter_
: A SQL expression that is applied to each matching value at the filter path.predicate> Supports MATCH_
function to indicate where the matched values are substituted in the filter predicate.PARAM_ <type>() Refer to Remarks for more information.
Return Type
A TINYINT
value.
Remarks
-
BSON_
returnsMATCH_ ANY() 1
if any value at the specified filter path evaluates the filter predicate totrue
. -
The filter path is matched as:
-
If the current element is a BSON document, the next key in the filter path is matched as a key in the document.
-
If the current element is an array, the next key in the filter path is either matched as a zero-based index in the array or a key of any document that is an element in the array.
-
If the next key in the filter path can be matched both as a BSON document and an array,
BSON_
matches the zero-based index in the array.MATCH_ ANY()
For example, both of the following queries 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 | +--------+
-
MATCH_ PARAM_ <type>()
-
MATCH_
functions can only be used within thePARAM_ <type>() <filter_
argument.predicate> -
The behavior of each
MATCH_
function is identical to the correspondingPARAM_ <type>() BSON_
function.EXTRACT_ <type>() Refer to BSON Functions for more information. -
The following
MATCH_
functions are supported:PARAM_ <type>() MATCH_
PARAM_ BIGINT_ STRICT() MATCH_
PARAM_ BOOL_ STRICT() MATCH_
PARAM_ BSON() MATCH_
PARAM_ DATETIME_ STRICT() MATCH_
PARAM_ DOUBLE_ STRICT() MATCH_
PARAM_ STRING_ STRICT()
Examples
The examples in this section use the following 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: June 11, 2024