couchdb - Passing an argument to map.js in a CouchApp view -
i have couchapp functions social network. have button which, when clicked user, creates , saves couchdb json document fields post_id (the id of post liked), user_id (the id of user liked post), , type (value "like," indicate document like).
alongside post, i'd indicate number of likes has received. map function looks this:
function(doc) { if (doc.type && doc.type == 'like') { emit(doc.post_id, 1); } }
and reduce, this:
function(post_ids, likes) { return sum(likes); }
my problem is, function returns sum of likes in posts in site. i'm thinking reduce.js fine , maybe tweak map.js accept post_id argument can retrieve likes post_id, how do that? post_id comes design document, of course, when "like" button clicked.
thanks.
you need group
view parameter. if supply no querystring parameters, default reduce entire contents of view. once apply group=true
, can use key
or startkey/endkey
pick out post id(s) need.
also, there built-in reduce functions can perform basic reduce operations more efficiently. (since compiled erlang code, not javascript source) put _count
in place of entire reduce.js
. (you can leave off 1
in emit
)
Comments
Post a Comment