3

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

Pavan Kumar Sunkara
  • 3,025
  • 21
  • 30
cschwarz
  • 1,195
  • 11
  • 24

2 Answers2

2

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.

Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90
2

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

smathy
  • 26,283
  • 5
  • 48
  • 68
  • Thanks, got that thing with the query-parameters. Nevertheless: How exactly does couchDB compare it's the keys of the map-results with startkey resp. endkey? – cschwarz Jul 10 '12 at 09:28
  • Good call, I guess I got hung up on the "variable from client" part of the question :) – Dominic Barnes Jul 10 '12 at 13:58
  • I'm not 100% sure what you're asking, so let me point you to two places in the docs: [the query options](http://wiki.apache.org/couchdb/HTTP_view_API?action=show&redirect=HttpViewApi#Querying_Options) and [the view collation info](http://wiki.apache.org/couchdb/View_collation) – smathy Jul 10 '12 at 17:00