Search code examples
couchbasesql++

Retrieving documents on keys using nested query


Started learning NIQL on couchbase. I am trying to retrieve multiple documents based on the keys using following nested query. Can't get it working. Is that even possible ?

SELECT * FROM Cart USE KEYS (
  SELECT META().id FROM Cart WHERE META().id LIKE "100%"
)

Solution

  • USE KEYS expecting array of strings. Subquery generates array of Object of strings. Use RAW in subquery to remove object when you projecting single field.

    You have one of the following options

    SELECT META().id, * FROM Cart WHERE META().id LIKE "100%";
    

    OR

    SELECT * FROM Cart USE KEYS (SELECT RAW META().id FROM Cart WHERE META().id LIKE "100%");
    

    You can checkout N1QL tutorial https://query-tutorial.couchbase.com/tutorial/#1