You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Wordit Ltd <wo...@googlemail.com> on 2010/12/06 18:28:55 UTC

How to pass parameters to views?

Can parameters be passed to views? I know you can use them in updaters.
I hoped this would work, but it did not:

function(doc, req) {
var myparam = req.query.myparam;
...

URL: .../view_name?myparam="foo"


I need to pass two parameters to a for loop which is pulling data from
an array/list.
I have data stored in the array "words".

function(doc) {
 for (i=0; i < doc.words.length;i++) {
  emit(i, doc.words[i]);
 }
}

This outputs ALL items in the array. I want to pass in A and B as
params to the for loop.
Can this be done, or is there another way to achieve it?

Thanks,

Marcus

Re: How to pass parameters to views?

Posted by Randall Leeds <ra...@gmail.com>.
On Mon, Dec 6, 2010 at 13:07, Wordit Ltd <wo...@googlemail.com> wrote:
> On Mon, Dec 6, 2010 at 8:23 PM, Randall Leeds <ra...@gmail.com> wrote:
>>
>> Oh, yeah. So, to answer your other question. Use a _list function.
>
> Thanks, I had not looked at lists yet. I placed all data in one array
> inside one document, rather than using separate docs for each item.
>
> Is there any performance difference to using a view on say 500 docs,
> or using a list function on a single doc containing the same 500 items
> in an array?
>

Not sure I have enough information to say. Depends on what processing
your list function does.
Remember that the input to a _list is actually a view, so do any
deterministic pre-processing in the view and filter in the _list.

> Thanks,
>
> Marcus
>

Re: How to pass parameters to views?

Posted by Wordit Ltd <wo...@googlemail.com>.
On Mon, Dec 6, 2010 at 8:23 PM, Randall Leeds <ra...@gmail.com> wrote:
>
> Oh, yeah. So, to answer your other question. Use a _list function.

Thanks, I had not looked at lists yet. I placed all data in one array
inside one document, rather than using separate docs for each item.

Is there any performance difference to using a view on say 500 docs,
or using a list function on a single doc containing the same 500 items
in an array?

Thanks,

Marcus

Re: How to pass parameters to views?

Posted by Randall Leeds <ra...@gmail.com>.
Oh, yeah. So, to answer your other question. Use a _list function.

On Mon, Dec 6, 2010 at 11:22, Randall Leeds <ra...@gmail.com> wrote:
> It cannot be done due to the nature of views and the way couchdb does
> map-reduce.
> View output should be deterministic and as such cannot depend on
> query-time parameters.
> This is what allows the index to be build incrementally and the reason
> why a _list function runs through the whole result set every time you
> query it but views can be retrieved with ?stale=ok and aren't re-run
> for every request.
>
> On Mon, Dec 6, 2010 at 09:28, Wordit Ltd <wo...@googlemail.com> wrote:
>> Can parameters be passed to views? I know you can use them in updaters.
>> I hoped this would work, but it did not:
>>
>> function(doc, req) {
>> var myparam = req.query.myparam;
>> ...
>>
>> URL: .../view_name?myparam="foo"
>>
>>
>> I need to pass two parameters to a for loop which is pulling data from
>> an array/list.
>> I have data stored in the array "words".
>>
>> function(doc) {
>>  for (i=0; i < doc.words.length;i++) {
>>  emit(i, doc.words[i]);
>>  }
>> }
>>
>> This outputs ALL items in the array. I want to pass in A and B as
>> params to the for loop.
>> Can this be done, or is there another way to achieve it?
>>
>> Thanks,
>>
>> Marcus
>>
>

Re: How to pass parameters to views?

Posted by Randall Leeds <ra...@gmail.com>.
It cannot be done due to the nature of views and the way couchdb does
map-reduce.
View output should be deterministic and as such cannot depend on
query-time parameters.
This is what allows the index to be build incrementally and the reason
why a _list function runs through the whole result set every time you
query it but views can be retrieved with ?stale=ok and aren't re-run
for every request.

On Mon, Dec 6, 2010 at 09:28, Wordit Ltd <wo...@googlemail.com> wrote:
> Can parameters be passed to views? I know you can use them in updaters.
> I hoped this would work, but it did not:
>
> function(doc, req) {
> var myparam = req.query.myparam;
> ...
>
> URL: .../view_name?myparam="foo"
>
>
> I need to pass two parameters to a for loop which is pulling data from
> an array/list.
> I have data stored in the array "words".
>
> function(doc) {
>  for (i=0; i < doc.words.length;i++) {
>  emit(i, doc.words[i]);
>  }
> }
>
> This outputs ALL items in the array. I want to pass in A and B as
> params to the for loop.
> Can this be done, or is there another way to achieve it?
>
> Thanks,
>
> Marcus
>