NULL Values and Uniqueness in B-Tree JSON Indexes

Function-based B-tree indexes do not store NULL values by default.

If a json_value expression used by the index returns NULL, that row is not indexed. As a result, predicates such as IS NULL and json_value expressions used in ORDER BY generally do not use the index.

In dot-notation syntax, SPARSE corresponds to NULL ON EMPTY and DENSE corresponds to ERROR ON EMPTY. Dot-notation indexes do not let you add a constant column, so use standard function-based syntax when you must index NULL values for ordering or IS NULL predicates.

Index NULL Values

To index NULL values, add a constant to the index. This increases index size but lets the index support NULL handling and ordering.

CREATE INDEX email_idx_with_nulls ON bookings b
  (json_value(data, '$.traveler.email.string()'
     ERROR ON ERROR NULL ON EMPTY), 1);

Use IS NULL when comparing with NULL.

Enforce Uniqueness

You can enforce uniqueness with UNIQUE.

DROP INDEX confirmationNo_idx;

CREATE UNIQUE MULTIVALUE INDEX confirmationNo_idx ON bookings b
  (b.data.reservations[*].confirmationNumber.string());
DROP INDEX email_idx;

CREATE UNIQUE INDEX email_idx ON bookings b
  (json_value(data, '$.traveler.email.string()'
     ERROR ON ERROR NULL ON EMPTY));

Use UNIQUE with Dot Notation

Dot-notation syntax also supports UNIQUE:

DROP INDEX email_idx;

CREATE JSON UNIQUE SINGLEVALUE INDEX email_idx ON bookings b
  (b.data.traveler.email.string());

Note: A unique index that also stores NULL values is typically not useful because it permits only one NULL entry.