Indexing Metadata Information

Couchbase Server allows indexing on metadata fields of any document, 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.

META( [ keyspace_expr ] ) [ .property ]

Description

Returns metadata for the document keyspace_expr.

You can use the META() function with a property when creating an index, for example META().id. The META() function does not require a keyspace parameter when creating an index, since it implicitly uses the keyspace being indexed.

Arguments

keyspace_expr

[Optional. Default is current keyspace.]

String or an expression that results in a keyspace or a document.

property

[Optional] The name of a single metadata property. The property name must be separated from the META() function by a dot (.) and may be one of the following:

cas

Value representing the current state of an item which changes every time the item is modified.

For details, see Concurrent Document Mutations.

expiration

Value representing a document’s expiration date. A value of 0 (zero) means no expiration date.

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

For details, see KV Operations.

flags

Value set by the SDKs for non-JSON documents.

For details, see Non-JSON Documents.

id

Value representing a document’s unique ID number.

type

Value for the type of document; currently only "json" is supported.

Return Value

JSON value of the document’s metadata.

Examples

Example 1. Find two documents that have no expiration date
Index
CREATE INDEX idx_expir ON `travel-sample` ( META().expiration );
Query
SELECT META().id, META().expiration
FROM `travel-sample`
WHERE META().expiration = 0
ORDER BY META().id
LIMIT 2;
Results
[
  {
    "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_id ON `travel-sample` (META().id );
Query
SELECT name, META().id
FROM `travel-sample`
WHERE META().id > "g"
LIMIT 2;
Results
[
  {
    "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_cas ON `travel-sample` ( META().cas );
Query
SELECT name, META().cas
FROM `travel-sample`
WHERE type="hotel"
ORDER BY META().cas DESC
LIMIT 2;
Results
[
        {
            "cas": 1503510338735374337,
            "name": "Hotel Formule 1"
        },
        {
            "cas": 1503510338734850048,
            "name": "Harbour Cottage Gardenstown"
        }
    ]