Indexing Metadata Information

Couchbase Server allows indexing on metadata fields of any document. These fields have existed before, and now version 5.0 has added support for indexing and querying 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() and meta().id functions when creating an index; and in this context, the meta() and meta().id functions do not require a parameter since they implicitly use 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
.cas

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

Indexable from Couchbase Server 5.0 For details, see java-sdk::concurrent-mutations-cluster.adoc.

.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.

Indexable from Couchbase Server 5.0 For details, see java-sdk::core-operations.adoc.

.flags

Value set by the SDKs for non-JSON documents.

For details, see java-sdk::nonjson.adoc.

.id

Value representing a document’s unique ID number.

Indexable from Couchbase Server 4.0

.type

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

Return Value

JSON value of the document’s metadata.

Example 1: Find two documents that have no expiration date.

CREATE INDEX idx_expir ON `travel-sample` ( META().expiration );

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".

CREATE INDEX idx_id ON `travel-sample` (META().id );

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.

CREATE INDEX idx_cas ON `travel-sample` ( META().cas );

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"
        }
    ]