Msg:#1284 of 1377Date:2014-04-21Subj:CouchDB Map Reduce Examples
Here are some example CouchDB Map/Reduce functions to some common things that I do. I felt that it would be nice to write these down for reference later and to help out others using CouchDB.
I want to find how many dealers are in each city:
// Map (doc) -> if doc.type == "Dealer" and doc.state emit(doc.state, 1) // Reduce (keys, values, rereduce) -> return sum(values) if rereduce keys.length
I want to find all dealers in CA
// Map (doc) -> if doc.type == "Dealer" and doc.state == "CA" emit(null, doc)
I want to show all inventory for an event sorted by sort_order.
// Map
(doc) ->
if doc.type == "Inventory" and doc.sort_order
emit([doc.sort_order, doc._id], doc)
Next