2

I am brand new to noSQL, couchDB, and mapreduce and need some help.

I have the same question discussed here {How to use reduce in Fauxton} but do not understand the answer:(.

I have a working map function:

function (foo) {
   if(foo.type == "blog post");
  emit(foo)
}

which returns 11 individual documents. I want to modify this to return foo.type along with a count of 1. I have tried:

function (doc) {
   if(doc.type == "blog post");
  return count(doc)
}

and "_count" from the Reduce panel, but clearly am doing something wrong as the View does not return anything.

Thanks in advance for any assistance or guidance!

Alexis Côté
  • 3,670
  • 2
  • 14
  • 30
CarCrazyBen
  • 1,066
  • 5
  • 14
  • 37

2 Answers2

9

In Fauxton, the Reduce step is kind of awkward and unintuitive to find.

  1. Select _count in the "Reduce (optional)" popup below where you type in your Map.
  2. Select "Save Document and then Build Index". That will display your map results.
  3. Find the "Options" button at the top next to a gears icon. If you see a green band instead, close the green band with the X.
  4. Select Options, then the "Reduce" check-circle. Select Run Query.
NSchorr
  • 875
  • 10
  • 13
  • 1
    Thank you SO much. I've spent several hours trying everything Google returns about Map-Reduce in CouchDb and saw this mentioned no where else. – Martin Bramwell Nov 12 '18 at 14:57
  • Martin Bramwell - hey you're welcome. Glad to help people here as this has been my main source of sanity. :) And yeah that thing drove me mad before I somehow figured it out. Good luck with it. – NSchorr Nov 15 '18 at 07:22
  • 1
    I'd be really curious about your take on a little mystery that's naggin' at the back of my brain. Whenever I read about "offline-first", client side databases, "native-like web apps", etc, PouchDB is listed as the premiere enabling toolkit for that ... yet there doesn't seem to be any more activity around PouchDB these days, the docs have landmines, and forum help seems only to come from other n00Bs like me. What's going on? – Martin Bramwell Nov 15 '18 at 21:08
  • Martin Bramwell - I have no idea. Not familiar with PouchDB. – NSchorr Dec 02 '18 at 02:38
  • 1
    Thank you. Now, I wonder what motivated that design choice. There must be a reason why, in Fauxton, the possibility to apply the reduce function is hidden like that. – John Smith Optional Jun 28 '19 at 00:09
  • Some guesses: not a design choice- no time to improve it; no money behind it, etc. – NSchorr Jul 01 '19 at 21:04
1

Map

So when you build a map function, you are literally creating a dictionnary or map which are key:value data structures.

Your map function should emit keys that you will query. You can also emit a value but if you intend to simply get the associated document, you don't have to emit any values. Why? Because there is a query parameter that can be used to return the document associated (?include_docs=true).

Reduce

Then, you can have reduce function which will be called for every result with the same keys. Every result with the same key will be processed through your reduce function to reduce the value.

Corrected example

So in your case, you want to map document the document per type I suppose.

You could create a function that emit documents that have the type property.

function(doc){
    if(doc.type)
        emit(doc.type);
}

If you query this view, you will see that the keys of each rows will be the type of the document. If you choose the _count reduce function, you should have the number of document per types.

When querying the view, you have to specify : group=true&reduce=true

Also, you can get all the document of type blog postby querying with those parameters : ?key="blog post"

Community
  • 1
  • 1
Alexis Côté
  • 3,670
  • 2
  • 14
  • 30