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

Querying by type


One of the most basic tasks when building applications is to find all records of a particular type. In the relational world, this effort is analogous to SELECT * FROM TableName. For example, you might need to display a list of all users in your system. For this query, we aren't concerned with any particular attribute of a document other than that it is a user document:

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

In this example, we'll simply check for the "user" type, and then emit a null key for each user document that is found. Since we didn't check for any property values other than the type, the index will contain all user documents. Again, the previous map function is similar to a SELECT * SQL query without a WHERE clause:

var view = client.GetView("users", "all_users");
foreach(var row in view)
{
  var user = row.GetItem() as User;
//do something with the user
}

In the preceding C# snippet, the all_users view from the users design document...