Search:

Search all manuals
Search this manual
Manual
Couchbase Server Manual 2.0
Community Wiki and Resources
Download Couchbase Server 2.0
Couchbase Developer Guide 2.0
Client Libraries
Couchbase Server Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
9.8 Querying Views
Chapter Sections
Chapters

9.8.2. Selecting Information

9.8.2.1. Selecting Compound Information by key or keys
9.8.2.2. Partial Selection and Key Ranges
9.8.2.3. Partial Selection with Compound Keys

Couchbase Server supports a number of mechanisms for selecting information returned by the view. Key selection is made after the view results (including the reduction function) are executed, and after the items in the view output have been sorted.

Important

When specifying keys to the selection mechanism, the key must be expressed in the form of a JSON value. For example, when specifying a single key, a string must be quoted ("string").

When specifying the key selection through a parameter, the keys must match the format of the keys emitted by the view. Compound keys, for example where an array or hash has been used in the emitted key structure, the supplied selection value should also be an array or a hash.

The following selection types are supported:

9.8.2.1. Selecting Compound Information by key or keys

If you are generating a compound key within your view, for example when outputting a date split into individualy year, month, day elements, then the selection value must exactly match the format and size of your compound key. The value of key or keys must exactly match the output key structure.

For example, with the view data:

JSON
{"total_rows":5693,"rows":[
{"id":"1310653019.12667","key":[2011,7,14,14,16,59],"value":null},
{"id":"1310662045.29534","key":[2011,7,14,16,47,25],"value":null},
{"id":"1310668923.16667","key":[2011,7,14,18,42,3],"value":null},
{"id":"1310675373.9877","key":[2011,7,14,20,29,33],"value":null},
{"id":"1310684917.60772","key":[2011,7,14,23,8,37],"value":null},
{"id":"1310693478.30841","key":[2011,7,15,1,31,18],"value":null},
{"id":"1310694625.02857","key":[2011,7,15,1,50,25],"value":null},
{"id":"1310705375.53361","key":[2011,7,15,4,49,35],"value":null},
{"id":"1310715999.09958","key":[2011,7,15,7,46,39],"value":null},
{"id":"1310716023.73212","key":[2011,7,15,7,47,3],"value":null}
]
}

Using the key selection mechanism you must specify the entire key value, i.e.:

?key=[2011,7,15,7,47,3]

If you specify a value, such as only the date:

?key=[2011,7,15]

The view will return no records, since there is no exact key match. Instead, you must use a range that encompasses the information range you want to output:

?startkey=[2011,7,15,0,0,0]&endkey=[2011,7,15,99,99,99]

This will output all records within the specified range for the specified date. For more information, see Section 9.8.2.3, “Partial Selection with Compound Keys”.

9.8.2.2. Partial Selection and Key Ranges

Matching of the key value has a precedence from right to left for the key value and the supplied startkey and/or endkey. Partial strings may therefore be specified and return specific information.

For example, given the view data:

"a",
 "aa",
 "bb",
 "bbb",
 "c",
 "cc",
 "ccc"
 "dddd"

Specifying a startkey parameter with the value "aa" will return the last seven records, including "aa":

"aa",
 "bb",
 "bbb",
 "c",
 "cc",
 "ccc",
 "dddd"

Specifying a partial string to startkey will trigger output of the selected values as soon as the first value or value greather than the specified value is identified. For strings, this partial match (from left to right) is identified. For example, specifying a startkey of "d" will return:

"dddd"

This is because the first match is identified as soon as the a key from a view row matches the supplied startkey value from right to left. The supplied single character matches the first character of the view output.

When comparing larger strings and compound values the same matching algorithm is used. For example, searching a database of ingredients and specifying a startkey of "almond" will return all the ingredients, including "almond", "almonds", and "almond essence".

To match all of the records for a given word or value across the entire range, you can use the null value in the endkey parameter. For example, to search for all records that start only with the word "almond", you specify a startkey of "almond", and an endkey of "almond\u02ad" (i.e. with the last latin character at the end). If you are using Unicode strings, you may want to use "\uefff".

startkey="almond"&endkey="almond\u02ad"

The precedence in this example is that output starts when 'almond' is seen, and stops when the emitted data is lexically greater than the supplied endkey. Although a record with the value "almond\02ad" will never be seen, the emitted data will eventually be lexically greater than "almond\02ad" and output will stop.

In effect, a range specified in this way acts as a prefix with all the data being output that match the specified prefix.

9.8.2.3. Partial Selection with Compound Keys

Compound keys, such as arrays or hashes, can also be specified in the view output, and the matching precedence can be used to provide complex selection ranges. For example, if time data is emitted in the following format:

[year,month,day,hour,minute]

Then precise date (and time) ranges can be selected by specifying the date and time in the generate data. For example, to get information between 1st April 2011, 00:00 and 30th September 2011, 23:59:

?startkey=[2011,4,1,0,0]&endkey=[2011,9,30,23,59]

The flexible structure and nature of the startkey and endkey values enable selection through a variety of range specifications. For example, you can obtain all of the data from the beginning of the year until the 5th March using:

?startkey=[2011]&endkey=[2011,3,5,23,59]

You can also examine data from a specific date through to the end of the month:

?startkey=[2011,3,16]&endkey=[2011,3,99]

In the above example, the value for the day element of the array is an impossible value, but the matching algorithm will identify when the emitted value is lexically greater than the supplied endkey value, and information selected for output will be stopped.

A limitation of this structure is that it is not possible to ignore the earlier array values. For example, to select information from 10am to 2pm each day, you cannot use this parameter set:

?startkey=[null,null,null,10,0]&endkey=[null,null,null,14,0]

In addition, because selection is made by a outputting a range of values based on the start and end key, you cannot specify range values for the date portion of the query:

?startkey=[0,0,0,10,0]&endkey=[9999,99,99,14,0]

This will instead output all the values from the first day at 10am to the last day at 2pm.

For more information and examples on formatting and querying this data, see Section 9.9.7, “Date and Time Selection”.