With Couchbase Server 2.0, you have two ways of fetching your documents: either by the unique key through the get method, or through Views. Since Views are more complex, let’s just look at a simple get first:
doc = client.get("aass_brewery-juleol") keys = ["foo", "bar"] docs = client.get(keys, :quiet => true)
In this case you will receve the Hash document you stored
earlier. If there no such key in the bucket, the exception
Couchbase::Error:NotFound will be raised. But
you can suppress all
NotFound errors by using
:quiet => true and the method will
nil instead. The
Couchbase::Bucket#get method can also accept
list of keys returning list of documents.
With Couchbase Server 2.0, the very powerful ability to query your documents across this distributed system through secondary indexes (Views) has been added to your toolbelt. This guide gets you started on how to use them through the Ruby SDK, if you want to learn more please refer to the chapter in the Couchbase Server 2.0 documentation.
Once you created your View in the UI, you can query it from the SDK in two steps. First, you grab the design document definition from the cluster, second query view with options you need and use results. In its simplest form, it looks like this:
# 1: Get the design document definition ddoc = client.design_docs["beer"] ddoc.views #=> ["brewery_beers", "by_location"] # 2: Query the view and use results ddoc.brewery_beers.each do |row| puts row.key puts row.value puts row.id puts row.doc end
Note that the view request won’t be executed until you
will try to access the results. This means that you can pass
view object (
ddoc.brewery_beers here) without
Views can be queried with a large amount of options to change
what the results of the query will contain. All supported
options are available as items in options Hash accepted either
by the view method or by
#each iterator on
the view. Here are some of them:
Used to define if the complete documents should be
fetched with the result (
Used to enable/disable the reduce function (if there
is one defined on the server).
Limit the number of results that should be returned.
Revert the sorting order of the result set.
stale (Boolean, Symbol)
Can be used to define the tradeoff between performance
and freshness of the data.
Now that we have our View information in place, we can issue the
query, which actually triggers the scatter-gather data loading
process on the Cluster. We can use it to iterate over the
results and print out some details (here is a more complete
example which also includes the full documents and only fetches
the first five results). The resulting information is
encapsulated inside the
view = client.design_docs["beer"].brewery_beers # Include all docs and limit to 5 view.each(:include_docs => true, :limit => 5) do |row| puts row.id # The full document (as a Hash) is available through row.doc end
In the logs, you can see the corresponding document keys automatically sorted (ascending):
21st_amendment_brewery_cafe 21st_amendment_brewery_cafe-21a_ipa 21st_amendment_brewery_cafe-563_stout 21st_amendment_brewery_cafe-amendment_pale_ale 21st_amendment_brewery_cafe-bitter_american