Book Image

Couchbase Essentials

Book Image

Couchbase Essentials

Overview of this book

Table of Contents (15 chapters)
Couchbase Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 IN address
SATISFIES LENGTH(addr) > 0 END

With a very slight change to the query, we can modify the behavior so that instead of returning breweries with a mix of empty and valid addresses in the address array, we return only those documents where...