Handling result sets
When you query a view, Couchbase Server generates an index which can contain zero or more results. Couchbase SDKs provide helper methods which enable you to iterate through the items in an index and perform operations on each individual result. For instance, going back to our blog example we performed a map function to get all the blog post dates and titles. In this case, we have a result set as follows:
KeyValue
"2012/07/30 18:12:10" "Move Today"
"2012/09/17 21:13:39" "Bought New Fridge"
"2012/09/25 15:52:20" "Paint Ball"
The result set returned by Couchbase Server inherits from Ruby Enumerable interface, and can therefore be treated like any other Enumerable object:
blog.recent_posts.each do |doc|
# do something
# with doc object
doc.key # gives the key argument of the emit()
doc.value # gives the value argument of the emit()
end
We can access each result in the result set with the each..do |value| block; in this example we output each key and value form the result set. Here is another example in.Net where the result set is provided as an enumerated value. This example is part of the sample beer application provided with your Couchbase install:
var view = client.GetView("beer", "by_name");
foreach (var row in view)
{
Console.WriteLine("Key: {0}, Value: {1}", row.Info["key"], row.Info["value"]);
}
In the first line we query a view stored in the design document beer called by_name. Then we output each item in the result set, which will give us a list of beer names for all beer documents. For more information about the sample application, see the individual Getting Started Guide and Language Reference for your chosen SDK at Develop with Couchbase.