Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CouchDB - Variables in map function

I am quite new to CouchDB and have a very basic question:

Is there any possibility to pass a variable from the client into the map function, e.g.:

function (doc, params) {
    if (doc.property > params.property) emit(doc, null);
}

Thanks for your help, Christian

like image 512
cschwarz Avatar asked Dec 04 '25 15:12

cschwarz


2 Answers

While Dominic's answer is true, the example in the actual question can probably be implemented as a map function with an appropriate key and a query that includes a startkey. So if you want the functionality that you show in your example you should change your view to this:

function(doc) {
  if( doc.property )
    emit( doc.property, null);
}

And then your query would become:

/db_name/_design/view_doc/_view/view_name?startkey="property_param"&include_docs=true

Which would give you what your example suggests you're after.

This is the key (puns are funny) to working with CouchDB: create views that allow you to select subsets of the view based on the key using either key, keys or some combination of startkey and/or endkey

like image 192
smathy Avatar answered Dec 06 '25 04:12

smathy


No, map functions are supposed to create indexes that always take the same input and yield the same output so they can remain incremental. (and fast)

If you need to do some sort of filtering on the results of a view, consider using a _list function, as that can take client-supplied querystring variables and use them in their transformation.

like image 43
Dominic Barnes Avatar answered Dec 06 '25 05:12

Dominic Barnes