Issue
I am working on the Apache CouchDB using the cURL. I have created a database added database files & created a view. I have made the following configurations for mapping the keys to the view.
function (doc) {
emit([doc.key1, doc.key2, doc.key3], doc);
}
I would like to access the database file using a combination of keys. i.e. In the above case, key1 is fixed in my URL's get request. but I don't have control over key2 & key3(sometimes I have key2 & rest I have key3).
Will you please help me to know, How can I access the database with a combination of key1-key2 OR key1-key3?
Solution
Have a look at this Guide with compound keys: https://docs.couchdb.org/en/stable/ddocs/views/collation.html#examples
Thus, you could try something like this:
startkey=[<val_key1>]&endkey[<val_key1>,{}]
or
startkey=[<val_key1>,<val_key2>]&endkey[<val_key1>,<val_key2>,{}]
If you use cURL, don't forget to encode the curly brackets or use option -g/--globoff (Passing a URL with brackets to curl).
EDIT:
- There is no space between the keys (write
[<val_key1>,{}]
instead of[<val_key1>, {}]
) - Likely, you have to create multiple views for each of your available key combination (key1-key2, key2-key3, key1-key3) combination because e.g.
[{},<val_key2>,<val_key3>]
does not work and will return you no rows. If you want to group by<val_key2>
or<val_key3>
you have to emit them first. (see also https://forums.couchbase.com/t/how-to-give-muliple-composite-keys-in-startkey-and-endkey-to-search-in-views/629)
Answered By - Rea Answer Checked By - Pedro (WPSolving Volunteer)