Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using 'limit' parameter in CouchDB View Map function

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...?

like image 491
tariq Avatar asked Apr 04 '12 15:04

tariq


People also ask

What is MAP reduce in CouchDB?

In order to retrieve data with CouchDB, we use a process called MapReduce, to create views. A view contains rows of data that is sorted by the row's key (you might use date as a key, for example, to sort your data based on the date). MapReduce is a combination of two concepts Map and Reduce.

Where are views stored in CouchDB?

CouchDB's views are stored in the B-tree file structure. Because of the way B-trees are structured, we can cache the intermediate reduce results in the non-leaf nodes of the tree, so reduce queries can be computed along arbitrary key ranges in logarithmic time. See Figure 1, “Comments map function”.

What are the primary benefits of implementing views in CouchDB?

CouchDB uses views as the primary tool for running queries and creating reports from stored document files. Views allow you to filter documents to find information relevant to a particular database process.


1 Answers

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.

like image 157
Sam Bisbee Avatar answered Nov 25 '22 06:11

Sam Bisbee