On 5/27/11 5:16 AM, Torstein Krause Johansen wrote:
Hi there,
On 27/05/11 16:01, Gabor Ratky wrote:
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).
Mhmm, ok, thanks for explaining this.
It means though, that for every unique time stamp that a_name has an
entry, there will be a corresponding count returned (like the three
you listed above).
Hence, if a_name has 1000 entries at slightly different times within
the time range I'm searching for (my created_at includes seconds), I
will get 1000 such entries back.
It really just depends on what you want to count/reduce/etc. If you only
want a count of the names (and don't want additional
granularity--name+year counts) then just return the name as the index.
If you want the count of names by year/month/day, etc, then return those
*after* the name, so you can add specificity by incrementing your
group_level param.
Alternatively, if you want to count *just* the names and *just* the
dates, you'll need two indexes ones for names and one for dates as you
can't "skip" the key groups (as your example tried to do with [{},...].
Basically, you'll need an additional view/index for each key you're
wanting to count + whatever output you want to make the counting more
granular (in this case, date).
Hope that helps,
Benjamin
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.
Ok, thanks a lot for your confirmation!
Cheers,
-Torstein