A newer version of this documentation is available.

View Latest

Indexing Metadata Information

  • reference
March 23, 2025
+ 12

Couchbase Server allows indexing on selected metadata fields, for example the expiration and CAS properties. This improves performance of queries involving predicates on the metadata fields, such as expired documents or recently modified documents.

Overview

The META() function enables you to return the metadata for a keyspace or document. To index a selected metadata field, you must use a nested expression containing the META() function and the required property, for example META().id.

The property name must be separated from the META() function by a dot (.) and only the following metadata properties can be indexed. If you attempt to build an index on a metadata field that is not indexable, an error is returned.

cas

Value representing the current state of an item which changes every time the item is modified. For details, refer to Concurrent Document Mutations.

expiration

Value representing a document’s expiration date. A value of 0 (zero) means no expiration date. For details, refer to KV Operations.

Note that this property gives correct results only when used in a Covered Index.

id

Value representing a document’s unique ID number.

The META() function does not require a keyspace parameter when creating an index, since it implicitly uses the keyspace being indexed.

Examples

Example 1. Find two documents that have no expiration date
Index
CREATE INDEX idx_airline_expire ON `travel-sample`.inventory.airline (META().expiration);
Query
n1ql
SELECT META().id, META().expiration FROM `travel-sample`.inventory.airline WHERE META().expiration = 0 ORDER BY META().id LIMIT 2;
Results
json
[ { "expiration": 0, "id": "airline_10" }, { "expiration": 0, "id": "airline_10123" } ]
Example 2. Find all documents whose meta ID tag starts with a letter higher than "g"
Index
CREATE INDEX idx_hotel_id ON `travel-sample`.inventory.hotel (META().id);
Query
n1ql
SELECT name, META().id FROM `travel-sample`.inventory.hotel WHERE META().id > "g" LIMIT 2;
Results
json
[ { "id": "hotel_10025", "name": "Medway Youth Hostel" }, { "id": "hotel_10026", "name": "The Balmoral Guesthouse" } ]
Example 3. Find the two most recently modified hotel documents
Index
CREATE INDEX idx_hotel_cas ON `travel-sample`.inventory.hotel (META().cas);
Query
n1ql
SELECT name, META().cas FROM `travel-sample`.inventory.hotel ORDER BY META().cas DESC LIMIT 2;
Results
json
[ { "cas": 1612962459766947800, "name": "The George Hotel" }, { "cas": 1612962459645378600, "name": "Texas Spring" } ]