couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gabor Ratky <rg...@rgabostyle.com>
Subject Re: Complex queries & results
Date Fri, 27 May 2011 08:01:46 GMT
The order of your components of your keys matter. There is no way to ignore the first element
of your key. You should instead reorder your keys based on what you're trying to count / query.

emit([created_at, a_name, one_id, another_id]);

and querying with:

?group=true&group_level=2&startkey=["2011-05-26"]&endkey=["2011-05-27", {}]

results in:

{
 "key": ["2011-05-26", "Lisa"],
 "value": 1
},
{
 "key": ["2011-05-26", "John"],
 "value": 2
},
{
 "key": ["2011-05-27", "John"],
 "value": 1
}

You can of course emit not just days, but also weeks, months, quarters if that's what you
always want. If it arbitrary and you need to aggregate the names afterwards from this smaller
set, yo should do it in the client (whoever calls CouchDB to get this information out).

tldr; The elements and order of the keys determines the order of the index and you can only
filter for a contiguous part of that index with startkey / endkey.

Gabor


On Friday, May 27, 2011 at 5:11 AM, Torstein Krause Johansen wrote:

> Hi all,
> 
> thanks for the great feedback.
> 
> On 26/05/11 19:26, Sean Copenhaver wrote:
> > Have you tried having a map function that emits the 'a_name' as a key then a
> > reduce function that uses the builtin sum(), then you query with group=true?
> 
> If I do:
> 
> emit([a_name, one_id, another_id, created_at]);
> 
> I can query it with (reduce just being the built in _count)
> 
> group=true&group_level=1
> 
> and by that get the dictionary I wanted:
> 
> {
>  "Lisa" : 1
>  "John" : 3
> }
> 
> I know need to make the start and endkey parameters ignore the first 
> element of my key as a_name is not a search parameter. I've tried with 
> this search for one_id=1, another_id=2 and date between 2011-05-26 and 
> 2011-05-27:
> 
> startkey=[{},1,2,"2011-05-26"]&endkey=[{},1,2,"2011-05-27"]
> 
> But don't get anything returned. Am I doing this wrong? Is there a 
> different way to make the view ignore the first element of the key? In 
> the coucdb.log I can see that my query parameters enter Couch the way I 
> intended (no BASH escaping going astray).
> 
> Cheers,
> 
> -Torstein


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message