1
votes

To query a couch db database you can sent a http GET request in the style of

host:5984/db/_design/some_view/_view/view1?key="foo"

To make a simple view named view1 to it I could create the map

function(doc){
  emit(doc.bar,doc);
}

That will produce a list of key-value pairs where doc.bar is the key and doc is the value. In this case it is a subset of the db where doc.bar = "foo"

It seems the emit function compares its parameter agains the key parameter passed with the URL request.

How could i get the value of key from the URL and do checks on it before it is passed to emit?

1

1 Answers

1
votes

MapReduce doesn't work like this.

Map (and reduce) functions are not called at each query for each object. They are called for each updated object (at query time).

This means that, as long as the "document" (aka object) is not changed, emitted keys and values are kept in the index. Cache management is why MapReduce is so efficient.

In other words, think about the index not as a query result but as the anticipation of all possible results.

Explaining further MapReduce in a text box is nearly impossible: I recommend you to follow tutorials and videos.

While you have understood the basics of MapReduce, you will learn how to query the index with CouchDB (with key, or startkey and endkey), and how to format the response (with custom parameters if you need them).