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

Null or missing properties


Throughout our exercises in writing map functions, it was common to test properties before attempting to emit them to a view index. N1QL also provides the capability to check for null or missing properties.

In JavaScript map functions, to check whether a property contains a null value, you simply compare the value to null:

if (doc.property == null) // do something

Missing properties are not null; rather, they don't exist. In order to check for a missing property with JavaScript, you can compare it to the undefined literal string or simply apply the bang (!) operator to your check. Using the latter test will allow for both a null check and a missing check:

if (! doc.property) // do something

There are two separate operators in N1QL used to test null and missing property values. The first query in the following snippet tests whether the value for style is Null:

SELECT *
FROM beer-sample
WHERE type = "beer"
AND style IS NULL

The second tests whether the style property was...