incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anand Chitipothu <anandol...@gmail.com>
Subject Re: Complex keys and ranges
Date Tue, 02 Nov 2010 00:10:55 GMT
2010/11/1 cdr53x <cdr53x@free.fr>:
> Hello,
>
> I've read all the docs/wiki again but cannot find anything related to views
> usage when keys are complex JSON objects.
>
> Especially, there are no explanations nor examples of how the ranges (
> startkey/endkey ) are supposed to behave with complex keys.
>
> For instance let's presume we have the a view with the following map
> function :
>
> function(doc) {
>    ...
>    emit({ 'date' : created_at  , 'author' : author}, null) ;
> }
>
>
> Would it be possible to "filter" on both date and author ?
>
> For instance get all the docs created in a given time range by a given
> author , by passing a range like  :
>
>  start_key : { 'date' : '201002030000.' , author : 'adams' }
>  end_key   : { 'date' : '201003030000.' , author : 'adams' }
>
> ?
>
> I have tried this however it does not seem to work that way, so if anyone
> has some info on how this should work, I'd really appreciate

You need to change your map function to emit [author, date] to be able
to search for for author and time-range.

    emit([author, created_at], null);

Now query for startkey=["adams", "201002030000"]&endkey=["adams",
"201003030000"].

Remember that couchdb view a just a btree, all it does it keeping the
view sorted by key. You need to write your map function such that the
data you want to query comes together in the sorted order, so that you
can query the view by range.

Anand

Mime
View raw message