Using JSON_EXISTS with B-Tree Indexes
Use JSON_EXISTS with B-tree JSON indexes when predicates match the indexed path expressions and data types.
JSON_EXISTS is commonly used with multivalue JSON indexes because it naturally targets arrays and repeated values. It can also use single-value JSON indexes when a predicate targets a single scalar value that matches the indexed expression.
Use JSON_EXISTS with a Multivalue JSON Index
Create a multivalue JSON index on confirmation numbers in the reservations array.
DROP INDEX confirmationNo_idx;
CREATE MULTIVALUE INDEX confirmationNo_idx ON bookings b
(b.data.reservations[*].confirmationNumber.string());
Query for documents where any reservation has the specified confirmation number.
SELECT data
FROM bookings
WHERE JSON_EXISTS(data,
'$?(@.reservations[*].confirmationNumber.string() == "FGU89A")');
The index targets confirmation numbers inside an array, so each document can contribute multiple index entries. The JSON_EXISTS predicate matches the same path and data type, which allows the optimizer to use the index. The query returns only documents that contain at least one matching confirmation number.
The query returns the following bookings document, where at least one reservation has confirmationNumber equal to FGU89A.
{
"_id": "507f192e811c19709de860ef",
"traveler": {
"first": "Jane",
"last": "Doe",
"status": "silver",
"email": "jane.doe@example.com",
"phone": "+1-555-123-4567"
},
"reservations": [
{
"type": "flight",
"flightNumber": "DL456",
"departureCity": "London",
"arrivalCity": "New York",
"departureTime": "2024-05-05T08:00:00",
"arrivalTime": "2024-05-06T12:00:00",
"airline": "Comet Airlines",
"seat": "22A",
"confirmationNumber": "FGU89A"
},
{
"type": "flight",
"flightNumber": "DL457",
"departureCity": "New York",
"arrivalCity": "London",
"departureTime": "2024-05-03T22:00:00",
"arrivalTime": "2024-05-04T10:00:00",
"airline": "Comet Airlines",
"seat": "14C",
"confirmationNumber": "FGU89A"
},
{
"type": "hotel",
"hotelName": "London Palace",
"location": "London",
"checkIn": "2024-05-04",
"checkOut": "2024-05-07",
"roomType": "Standard Room",
"confirmationNumber": "LON67890"
}
],
"notes": "Traveler prefers window seats on a flight and requests accommodations that tend to be quieter with fewer street-side facing windows. Special dietary requirement is for vegetarian meals. Likes points program."
}
Use bind variables to avoid repeated parses. The following query returns the same result.
SELECT data
FROM bookings
WHERE JSON_EXISTS(data,
'$?(@.reservations[*].confirmationNumber.string() == $var1)'
PASSING 'FGU89A' AS "var1");
Use JSON_EXISTS with a Single-Value JSON Index
Create a single-value JSON index on the traveler email field.
DROP INDEX email_idx;
CREATE INDEX email_idx ON bookings b
(json_value(data, '$.traveler.email.string()'
ERROR ON ERROR NULL ON EMPTY));
Query using JSON_EXISTS with a matching path and type.
SELECT data
FROM bookings
WHERE JSON_EXISTS(data,
'$.traveler?(@.email.string() == $var1)'
PASSING 'jane.doe@example.com' AS "var1");
A single-value JSON index targets a single scalar email value, and the JSON_EXISTS predicate filters on the same path and data type, which allows the optimizer to use the index. The query returns the full bookings document where the traveler email matches the bind variable value.
DATA
----
{"_id":"507f192e811c19709de860ef","traveler":{"first":"Jane","last":"Doe","status":"silver","email":"jane.doe@example.com","phone":"+1-555-123-4567"},"reservations":[{"type":"flight","flightNumber":"DL456","departureCity":"London","arrivalCity":"New York","departureTime":"2024-05-05T08:00:00","arrivalTime":"2024-05-06T12:00:00","airline":"Comet Airlines","seat":"22A","confirmationNumber":"FGU89A"},{"type":"flight","flightNumber":"DL457","departureCity":"New York","arrivalCity":"London","departureTime":"2024-05-03T22:00:00","arrivalTime":"2024-05-04T10:00:00","airline":"Comet Airlines","seat":"14C","confirmationNumber":"FGU89A"},{"type":"hotel","hotelName":"London Palace","location":"London","checkIn":"2024-05-04","checkOut":"2024-05-07","roomType":"Standard Room","confirmationNumber":"LON67890"}],"notes":"Traveler prefers window seats on a flight and requests accommodations that tend to be quieter with fewer street-side facing windows. Special dietary requirement is for vegetarian meals. Likes points program."}
Use JSON_EXISTS with a Composite Index
Create a composite index on last name and first name.
DROP INDEX name_idx;
CREATE INDEX name_idx ON bookings
(json_value(data, '$.traveler.last' ERROR ON ERROR NULL ON EMPTY),
json_value(data, '$.traveler.first' ERROR ON ERROR NULL ON EMPTY));
Query using JSON_EXISTS with matching predicates.
SELECT data
FROM bookings
WHERE JSON_EXISTS(data,
'$.traveler?(@.last.string() == $var1 && @.first.string() == $var2)'
PASSING 'Doe' AS "var1", 'Jane' AS "var2");
The composite index can be used when the predicate constrains the leading indexed fields using matching paths and data types. The query returns the full bookings document where both last name and first name match the bind variables.
DATA
----
{"_id":"507f192e811c19709de860ef","traveler":{"first":"Jane","last":"Doe","status":"silver","email":"jane.doe@example.com","phone":"+1-555-123-4567"},"reservations":[{"type":"flight","flightNumber":"DL456","departureCity":"London","arrivalCity":"New York","departureTime":"2024-05-05T08:00:00","arrivalTime":"2024-05-06T12:00:00","airline":"Comet Airlines","seat":"22A","confirmationNumber":"FGU89A"},{"type":"flight","flightNumber":"DL457","departureCity":"New York","arrivalCity":"London","departureTime":"2024-05-03T22:00:00","arrivalTime":"2024-05-04T10:00:00","airline":"Comet Airlines","seat":"14C","confirmationNumber":"FGU89A"},{"type":"hotel","hotelName":"London Palace","location":"London","checkIn":"2024-05-04","checkOut":"2024-05-07","roomType":"Standard Room","confirmationNumber":"LON67890"}],"notes":"Traveler prefers window seats on a flight and requests accommodations that tend to be quieter with fewer street-side facing windows. Special dietary requirement is for vegetarian meals. Likes points program."}
Use JSON_EXISTS with IN
JSON_EXISTS supports array comparisons with IN, which is useful when matching any value from a list.
SELECT data
FROM bookings
WHERE JSON_EXISTS(data,
'$?(@.reservations[*].confirmationNumber.string()
in ("FGU89A", "FGU89B", "FGU89C"))');
This query returns documents where any reservation confirmation number matches one of the listed values. The output is the full bookings document for the matching traveler.
DATA
----
{"_id":"507f192e811c19709de860ef","traveler":{"first":"Jane","last":"Doe","status":"silver","email":"jane.doe@example.com","phone":"+1-555-123-4567"},"reservations":[{"type":"flight","flightNumber":"DL456","departureCity":"London","arrivalCity":"New York","departureTime":"2024-05-05T08:00:00","arrivalTime":"2024-05-06T12:00:00","airline":"Comet Airlines","seat":"22A","confirmationNumber":"FGU89A"},{"type":"flight","flightNumber":"DL457","departureCity":"New York","arrivalCity":"London","departureTime":"2024-05-03T22:00:00","arrivalTime":"2024-05-04T10:00:00","airline":"Comet Airlines","seat":"14C","confirmationNumber":"FGU89A"},{"type":"hotel","hotelName":"London Palace","location":"London","checkIn":"2024-05-04","checkOut":"2024-05-07","roomType":"Standard Room","confirmationNumber":"LON67890"}],"notes":"Traveler prefers window seats on a flight and requests accommodations that tend to be quieter with fewer street-side facing windows. Special dietary requirement is for vegetarian meals. Likes points program."}