I’m exploring CouchDB this morning, and am playing with a document schema that looks something like this:
{
"post_id": 1,
"date": "1/1/2011",
"body": "ur blog is awesome!"
}
I’d like to be able to build a view that gives me both a current count of comments for a given post_id as well as a ‘last commented on’ field that tells me when the most recent comment was made.
I have played around enough to do the map / reduce to get the comment count, but do not see any way to include the ‘last commented on’ field. I’m thinking that I’ll have to build two separate views here, but would like to know if it is possible to get this information in one trip instead of two.
My current attempt at getting the comment count:
map: function(doc) {
emit(doc.post_id, 1);
}
reduce: "_count"
Actually, I think I may have it.
If I switch my date to an epoch, I can then use the _stats reduce function to get back what I need.
Of note, _stats will return a max value as well as a count, which will represent my most recent comment date and total count respectively.