JSON key prefix and value search in couchbase

Viewed 121

In couchbase I will I have data like

"meta_info": {
  "tag1": 584254,
  "tag2": 0,
  "tag3": 1,
  "tag4": 2,
  "tag5": 4,
  "tag6": 4,
  "tag7": 6,
  "tag8": 6,
  "tag9": 1
}

And I want to match any key that starts with tag with value X

Is there any way to do this in N1QL query?

1 Answers

I found the N1QL query satisfied my requirements:

SELECT meta_info FROM `test-bucket` 
where type="result" and any k in OBJECT_PAIRS(meta_info) satisfies k.name like "tag%" and k.val=0 END;

Better performance can be achieved with using a flex index:

SELECT meta_info FROM `test-bucket` USE INDEX (USING FTS, USING GSI) 
where type="result" and any k in OBJECT_PAIRS(meta_info) satisfies k.name like "tag%" and k.val=0 END;

See documentation about creating a flex index: https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/flex-indexes.html

Related