learning mapreduce in Fauxton

755 Views Asked by At

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!

2

There are 2 best solutions below

0
On BEST ANSWER

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"

6
On

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.