Using 'limit' parameter in CouchDB View Map function

couchdb, mapreduce

Solution

The answer to your specific question is "no".

The map function gets applied to every document in the database and the reduce function, if defined, is applied to every reduce result. Think pre-computing.

The query parameters you provide in the URL is applied to the B+Tree that your MapReduce functions build. For example, if you say `?limit=5` then the five left most leafs in the tree are used as the results. Or if you say `?limit=5&descending=true` then the five right most leafs in the tree are used as the results.

However, what you are trying to accomplish by doing a `?limit=5` in the Map function might be accomplished in a different way. For example, your application could include something in the documents that made them get conditionally included in the results. Or enforce that only five documents are flagged to be in the index, though that'd be cumbersome and, depending on your database's size, costly.

Cheers.

Problem

I am aware that through the REST API, one can specify the 'limit' parameter (e.g. ?limit=5) in order to limit the number of results returned from a given view in CouchDB. My question is whether or not there is a way to do this inside the map function itself inside the view...?

Original source