Search code examples
databasecouchbasesql++

N1QL query for fetching a document where the expected field is a part of a list


I am fairly new to N1QL queries. I have some documents like this.

{
   "object":[
      {
         "x":"a",
         "y":"b"
      },
      {
         "x":"c",
         "y":"d"
      }
   ]
}

Now I want to fetch all documents where "x"="a" in any of the list members. How can I achieve that in couchbase using N1Ql query?


Solution

  • The query should look like this:

    SELECT * FROM bucketName WHERE ANY o IN object SATISFIES o.x='a' END
    

    ANY and SATISFIES are collections operators, and there are other ones you can use, including EVERY. You can read more about it in the Couchbase documentation on Collection Operators.