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

Compound indexes


Writing an AND query requires a different approach than what we used for our OR query. If you wanted to perform the analogue of a SQL query with multiple values in a WHERE clause, you'd need to structure your view keys in such a way as to allow your application to supply multiple values as one key parameter value:

SELECT * 
FROM Users 
WHERE LastName = 'Soprano' AND FirstName = 'Tony'

In the preceding SQL statement, we are able to have values for both LastName and FirstName. One approach would be to create a delimited key in our index like this:

function(doc, meta) {
  if (doc.type == "user" && doc.firstName && doc.lastName) {
    emit(doc.lastName + "," + doc.firstName, null);
  }
}

This map function emits a key in the form of "Soprano,Tony" to the index. When querying the index, the client application would concatenate the last name, a comma, and the first name. The resulting string would be provided as the argument for the key parameter.

Obviously, it's not...