Hi,
I have these documents :
{ "created_at": "20100301", "tag": "foo" },
{ "created_at": "20100301", "tag": "bar" },
{ "created_at": "20100301", "tag": "foo-bar" },
{ "created_at": "20100302", "tag": "foo" }
and what I want is to retrieve the documents within a date range and count
how many times does each tag appear globally, not just by its date. I should
get something like this:
{ "foo" : 2, "bar" : 1, "foo-bar" : 1}
So, in the first attempt, I wrote the following map/reduce functions:
// map
function(doc) {
emit([doc.created_at,doc.tag],1);
}
// reduce
function (key, values, rereduce) {
return sum(values);
}
Obviously this didn't work as the documents are grouped by the whole key and
if I set the group_level to 1, the documents are grouped only by the date:
/_design/tags/_view/popular?startkey=["20100301",null]&endkey=["20100302",{}]&group=true&group_level=1
{"rows":[
{"key":["20100301"],"value":3},
{"key":["20100302"],"value":1}
]}
Then I changed the emit function by setting the tag as the first position
for the key:
...
emit([doc.tag,doc.created_at],1);
Now I can group the results and get how many times each tag appears:
/_design/tags/_view/popular?group=true&group_level=1
{"rows":[
{"key":["bar"],"value":1},
{"key":["foo"],"value":2},
{"key":["foo-bar"],"value":1}
]}
The problem with this, is that I can't restrict the query to certain date
ranges as I get always all the documents in spite of the second part of the
key:
/_design/tags/_view/popular?group=true&group_level=1&startkey=[null,"20100302"]&endkey=[{},"20100302"]
{"rows":[
{"key":["bar"],"value":1},
{"key":["foo"],"value":2},
{"key":["foo-bar"],"value":1}
]}
when I should get something like
{"rows":[
{"key":["foo"],"value":1}
]}
I was wondering if this is the normal collation behaviour or I'm missing
anything and if there is any way to achieve this. I also tried to add the
collation option to 'raw' in the view definition just in case, but I got the
same results.
Thanks in advance.
Regards
--
def dagi3d(me)
case me
when :web then "http://dagi3d.net"
when :twitter then "http://twitter.com/dagi3d"
end
end