Using the "limit" parameter in the CouchDB View Map function

I know that through the REST API you can specify the parameter "limit" (for example,? Limit = 5) to limit the number of results returned from this view in CouchDB.

My question is, is there a way to do this inside the map function itself inside the view ...?

+6
source share
3 answers

The answer to your specific question is no.

The map function is applied to each document in the database, and the reduction function, if defined, is applied to each reduction result. Think about preliminary calculations.

The query parameters that you specify in the URL apply to the B + tree, which your MapReduce functions build. For example, if you say ?limit=5 , then five left leaves in the tree will be used as the results. Or, if you say ?limit=5&descending=true , the five right leaves in the tree are used as results.

However, what you are trying to accomplish by doing ?limit=5 in the Map function can be done in a different way. For example, your application may include something in documents that caused them to receive conditional inclusion in the results. Or make sure that only five documents are marked in the index, although this would be cumbersome and expensive depending on the size of your database.

Greetings.

+5
source

If you UUIDs are sequential (you can check your configurations for this), you could use this to view the first five documents yourself.

0
source

This is not possible for the map function. Because the map will process each document separately and, possibly, simultaneously on any separate node. He generally cannot know about another document.

But I think you want, perhaps with reduce . the reduction will be performed after the card function does everything and gets the whole result. So you can change the result of map any way

Simply put, you could use reduce to group strings with the same key, then you could trim each group to have only 5 results (in rereduce , if I remember it correctly)

0
source

Source: https://habr.com/ru/post/912408/


All Articles