Working with collections

N1QL provides a means to succinctly query collections within a document. Recall that to examine nested collections in a map function, you used to run a for loop over the items in that collection. To achieve similar results in N1QL queries, you can filter a collection using the ANY operator.

For example, if we continue to use the address property of brewery documents, we can search for only those addresses that are not empty. In the following example, we're checking the length of each address string as our condition. Note that if a document contained two addresses, where one was valid and another was an empty string, the condition would still be satisfied:

SELECT address FROM beer-sample WHERE type = "brewery" AND ANY addr ...

Get Couchbase Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.