You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Andru Vallance <an...@tinymighty.com> on 2013/10/26 12:06:56 UTC
Skipping complex key values in a view function
Is there a way to skip a value in a complex key (i.e. an array key) when using startkey and end key? That is, is there a way to match any value for an item in the array?
I have a document with a place_id, plant_id, date_created and date_modified. Sometimes I want all docs with place_id=X and plant_id=Y, where other times I only want place_id=X and any plant_id; sometimes I will want to specify a creation and/or modification date, other times this will not be relevant. Since these values are all user defined, and it's likely more properties will be added in the future, I don't think it's viable to use multiple views specific to each use case.
I'm currently using a single view map emitting multiple rows with null values to allow me to specify a null value in my startkey/endkey values.
function(doc){
if(doc.type==='planting'){
emit([doc.place_id, doc.plant_id, doc.date_created, doc.date_modified], null);
emit([doc.place_id, null, doc.date_created, doc.date_modified], null);
emit([null, null, doc.date_created, doc.date_modified], null);
}
}
Is there a more elegant way I can do this without the multiple row emits?
(note: I'm actually using PouchDB and replicating to CouchDB, so this map query is actually being run in the browser with PouchDB)
Thanks
Andru Vallance
Re: Skipping complex key values in a view function
Posted by Robert Newson <rn...@apache.org>.
"Is there a way to skip a value in a complex key (i.e. an array key)
when using startkey and end key? That is, is there a way to match any
value for an item in the array?"
Through a list function, you could, but not otherwise. A view is a
linear sequence of rows ordered by the full key, couchdb can only
efficiently look up a key or return all rows falling between two keys.
There's no such thing as a "complex" key in couchdb, it's just that
keys can be arrays and couchdb has a declared scheme for ordering of
arrays.
B.
On 26 October 2013 11:06, Andru Vallance <an...@tinymighty.com> wrote:
> Is there a way to skip a value in a complex key (i.e. an array key) when using startkey and end key? That is, is there a way to match any value for an item in the array?
>
> I have a document with a place_id, plant_id, date_created and date_modified. Sometimes I want all docs with place_id=X and plant_id=Y, where other times I only want place_id=X and any plant_id; sometimes I will want to specify a creation and/or modification date, other times this will not be relevant. Since these values are all user defined, and it's likely more properties will be added in the future, I don't think it's viable to use multiple views specific to each use case.
>
> I'm currently using a single view map emitting multiple rows with null values to allow me to specify a null value in my startkey/endkey values.
>
> function(doc){
> if(doc.type==='planting'){
> emit([doc.place_id, doc.plant_id, doc.date_created, doc.date_modified], null);
> emit([doc.place_id, null, doc.date_created, doc.date_modified], null);
> emit([null, null, doc.date_created, doc.date_modified], null);
> }
> }
>
> Is there a more elegant way I can do this without the multiple row emits?
>
> (note: I'm actually using PouchDB and replicating to CouchDB, so this map query is actually being run in the browser with PouchDB)
>
> Thanks
> Andru Vallance
Re: Skipping complex key values in a view function
Posted by Jens Alfke <je...@couchbase.com>.
On Oct 26, 2013, at 3:06 AM, Andru Vallance <an...@tinymighty.com> wrote:
> I have a document with a place_id, plant_id, date_created and date_modified. Sometimes I want all docs with place_id=X and plant_id=Y, where other times I only want place_id=X and any plant_id; sometimes I will want to specify a creation and/or modification date, other times this will not be relevant.
This should work with a single emit, unless I’m misunderstanding you. The trick is to use an empty object — {} — as a placeholder for the end of a range, since it will sort after any scalar or array value.
>From your example:
> emit([doc.place_id, doc.plant_id, doc.date_created, doc.date_modified], null);
To query for all docs with place_id=X and plant_id=Y, use
startkey = [place_id, plant_id]
endkey = [place_id, plant_id, {}]
To query for place_id=X and any plant_id, use
startkey = [place_id]
endkey = [place_id, {}]
—Jens