Indexing Metadata Information

Indexing Metadata Information

(Introduced in Couchbase Server 5.0)

Using Couchbase Server 5.0, both the META().cas and META().expiration properties can be used while creating indexes along with the meta().id property that was supported in earlier versions. These properties can be helpful for faster results and better performance. For example, if you want to run a query for all documents that had expired today or for all documents that were modified today, use the META().expiration property when creating an index.

In the following example, we created a cas index idx_cas on the `travel-sample` bucket for the property META().cas of any given document.
CREATE INDEX idx_cas
ON `travel-sample` ( META().cas, META().expiration )
Now use the idx_cas index in a N1QL query to retrieve documents as shown below:
SELECT META().id, META().cas
FROM `travel-sample`
WHERE META().cas > 0 LIMIT 2;

[
        {
            "cas": 1490105470888050688,
            "id": "airline_10"
        },
        {
            "cas": 1490105470888050688,
            "id": "airline_10123"
        }
    ]
In the following example, create an index idx_exp on the `travel-sample` bucket for the property meta().expiration of any given document.
CREATE INDEX idx_exp
ON `travel-sample` ( META().expiration )
Now use the idx_exp index in a N1QL query to retrieve some documents as shown below:
SELECT META().id, META().expiration
FROM `travel-sample`
WHERE META().expiration = 0
LIMIT 4;
[
  {
    "expiration": 0,
    "id": "airline_10"
  },
  {
    "expiration": 0,
    "id": "airline_10123"
  },
  {
    "expiration": 0,
    "id": "airline_10226"
  },
  {
    "expiration": 0,
    "id": "airline_10642"
  }
]

For more details about META(), see Miscellaneous Utility Functions.