How to pass parameters to the map function?

959 views
Skip to first unread message

SriramV

unread,
Mar 26, 2012, 10:01:30 AM3/26/12
to CouchDB-Python
Hello,

I have built a view where in my map function looks as follows :

function(doc){
//sender is an argument sent from the caller of
//databaseb.view() function

if(doc.from==sender){
emit(doc.type,{ 'from':doc.from,
'to':doc.to
})
}
}

The idea is to collect the docs whose 'from' field matches the
variable 'sender'. I am trying to pass the value of sender through
database::view() call as follows :

self.db.view('<view_id>', keys=['m...@mydomain.com']).

It's not working. I am not sure what is the right way to pass a
parameter to the map function in my view. Please help!

Thanks!

Alexander Shorin

unread,
Mar 28, 2012, 3:25:06 AM3/28/12
to couchdb...@googlegroups.com
Hi,

> self.db.view('<view_id>', keys=['m...@mydomain.com']).

Code looks fine, however I don't expect that you have doc.type with
same value. Keys parameter retrieves all view records that has same
value in key field(first emit value). If you need to view by email
addresses, you have to emit those values as key to build index on
them.

e.g.
function(doc){
if(doc.from==sender){ // probably it have to be `doc.type == 'sender'` ?
emit(doc.from, {'to': doc.to, 'from': doc.from});
emit(doc.to, {'to': doc.to, 'from': doc.from});
}
}

so with this function you may access view by email address key to get
all records about sender/receiver. If you need to split these values,
so you have to emit some additional parameter in key (as ['from,
doc.from]) or group keys with reduce function.

Hope that I've understood your problem right, sorry if not and correct
me then(: Probably, example of document that should be yielded right
could make clear some details.

--
,,,^..^,,,

> --
> You received this message because you are subscribed to the Google Groups "CouchDB-Python" group.
> To post to this group, send email to couchdb...@googlegroups.com.
> To unsubscribe from this group, send email to couchdb-pytho...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/couchdb-python?hl=en.
>

SriramV

unread,
Mar 29, 2012, 3:00:34 AM3/29/12
to CouchDB-Python
Hi

Thanks for your reply. My question was, within the map function, how
to access the list of parameters that are sent through self.db.view
function.

In the example code snippet, I had passed '..@domain.com' in a list. I
want to access the list within the map function. Because map function
receives only the document. I want to know how to receive the
arguments.

I tried using arguments[1] within the map function, but could not.

Code snippet :

//map function
function(doc,options) {
emit(doc[options], options); //can't get the {param:value} sent by
the caller
}

//python code to execute the view
self.db.view(name=<view>,wrapper=None,options={'param':'value'})

Thanks!
On Mar 28, 12:25 pm, Alexander Shorin <kxe...@gmail.com> wrote:
> Hi,
>
> > self.db.view('<view_id>', keys=['...@mydomain.com']).
> > self.db.view('<view_id>', keys=['...@mydomain.com']).

Eli Stevens (Gmail)

unread,
Mar 29, 2012, 12:17:29 PM3/29/12
to couchdb...@googlegroups.com
On Thu, Mar 29, 2012 at 12:00 AM, SriramV <mailto...@gmail.com> wrote:
> Thanks for your reply. My question was, within the map function, how
> to access the list of parameters that are sent through self.db.view
> function.

That's not how map functions work; the results of map are precomputed
and cached. There's no way to change their behavior at query time.
Note that this is a property of couchdb, not couchdb-python. Check
out:

http://wiki.apache.org/couchdb/HTTP_view_API

Cheers,
Eli

Reply all
Reply to author
Forward
0 new messages