You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Jehan Bihin <sq...@gmail.com> on 2009/09/02 18:25:13 UTC

View to find someone in a list in a document

Hi all,

(sorry for my english)
I have a document having a list of persons :

...
"users":{"membre-joouul_hotmail.com":["createur"],"membre-killan_daaboo.net
":["moderateur"]}
...

Each persons have some roles.
My key is the membre like 'membre-joouul_hotmail.com'
And i want to list all document having my key present in the users list.

Is it possible and correct to have it ?
Can you guide me ? After reading here and the site I found nothing talking
on that.

Thanks in advance,

Jehan

Re: View to find someone in a list in a document

Posted by Jehan Bihin <sq...@gmail.com>.
Thanks a lot ! I'll try it.


2009/9/2 Simon Metson <si...@googlemail.com>

> Ok, so use that map function and query it as:
>
> http://localhost:5984/test/_design/users/_view/user?key=<user_name>
>
> That will give you back all the documents that have <user_name> in the
> users list.
> Cheers
> Simon
>
>
>
> On 2 Sep 2009, at 18:12, Jehan Bihin wrote:
>
>  Hi Simon,
>>
>> Thanks for your help.
>>
>> I need the document if the key is present in the list.
>>
>> Thanks,
>>
>> Jehan
>>
>>
>> 2009/9/2 Simon Metson <si...@googlemail.com>
>>
>>  Hi,
>>>      Do you mean you want to emit each user as a key? Something like:
>>>
>>> function(doc) { for (v in doc.users) { emit(doc.users[v], doc); }
>>>
>>> as a map would do that.
>>> Cheers
>>> Simon
>>>
>>>
>>> On 2 Sep 2009, at 17:25, Jehan Bihin wrote:
>>>
>>> Hi all,
>>>
>>>>
>>>> (sorry for my english)
>>>> I have a document having a list of persons :
>>>>
>>>> ...
>>>> "users":{"membre-joouul_hotmail.com":["createur"],"membre-
>>>> killan_daaboo.net
>>>> ":["moderateur"]}
>>>> ...
>>>>
>>>> Each persons have some roles.
>>>> My key is the membre like 'membre-joouul_hotmail.com'
>>>> And i want to list all document having my key present in the users list.
>>>>
>>>> Is it possible and correct to have it ?
>>>> Can you guide me ? After reading here and the site I found nothing
>>>> talking
>>>> on that.
>>>>
>>>> Thanks in advance,
>>>>
>>>> Jehan
>>>>
>>>>
>>>
>>>
>

Re: View to find someone in a list in a document

Posted by Simon Metson <si...@googlemail.com>.
Ok, so use that map function and query it as:

http://localhost:5984/test/_design/users/_view/user?key=<user_name>

That will give you back all the documents that have <user_name> in the  
users list.
Cheers
Simon


On 2 Sep 2009, at 18:12, Jehan Bihin wrote:

> Hi Simon,
>
> Thanks for your help.
>
> I need the document if the key is present in the list.
>
> Thanks,
>
> Jehan
>
>
> 2009/9/2 Simon Metson <si...@googlemail.com>
>
>> Hi,
>>       Do you mean you want to emit each user as a key? Something  
>> like:
>>
>> function(doc) { for (v in doc.users) { emit(doc.users[v], doc); }
>>
>> as a map would do that.
>> Cheers
>> Simon
>>
>>
>> On 2 Sep 2009, at 17:25, Jehan Bihin wrote:
>>
>> Hi all,
>>>
>>> (sorry for my english)
>>> I have a document having a list of persons :
>>>
>>> ...
>>> "users":{"membre-joouul_hotmail.com":["createur"],"membre-
>>> killan_daaboo.net
>>> ":["moderateur"]}
>>> ...
>>>
>>> Each persons have some roles.
>>> My key is the membre like 'membre-joouul_hotmail.com'
>>> And i want to list all document having my key present in the users  
>>> list.
>>>
>>> Is it possible and correct to have it ?
>>> Can you guide me ? After reading here and the site I found nothing  
>>> talking
>>> on that.
>>>
>>> Thanks in advance,
>>>
>>> Jehan
>>>
>>
>>


Re: View to find someone in a list in a document

Posted by Jehan Bihin <sq...@gmail.com>.
Hi Simon,

Thanks for your help.

I need the document if the key is present in the list.

Thanks,

Jehan


2009/9/2 Simon Metson <si...@googlemail.com>

> Hi,
>        Do you mean you want to emit each user as a key? Something like:
>
> function(doc) { for (v in doc.users) { emit(doc.users[v], doc); }
>
> as a map would do that.
> Cheers
> Simon
>
>
> On 2 Sep 2009, at 17:25, Jehan Bihin wrote:
>
>  Hi all,
>>
>> (sorry for my english)
>> I have a document having a list of persons :
>>
>> ...
>> "users":{"membre-joouul_hotmail.com":["createur"],"membre-
>> killan_daaboo.net
>> ":["moderateur"]}
>> ...
>>
>> Each persons have some roles.
>> My key is the membre like 'membre-joouul_hotmail.com'
>> And i want to list all document having my key present in the users list.
>>
>> Is it possible and correct to have it ?
>> Can you guide me ? After reading here and the site I found nothing talking
>> on that.
>>
>> Thanks in advance,
>>
>> Jehan
>>
>
>

Add query arguments to #httpd

Posted by Michael McCaffrey <mm...@ittvis.com>.
You all helped me earlier in the week so here's another...

I want to call a view from an erlang function which I can get to work by
changing the path_parts on a #httpd such as this:

NewReq=Req#httpd{path_parts=[_DbName, _Design, DesignName, <<"_view">>,
<<"myView">>]},

couch_httpd_view:handle_view_req(NewReq, Db);

This works and returns all the docs from _view/myView.

Now, how do I go about adding a query argument such as ?key="someValue"
to the #httpd? I see where couch_httpd_view:handle_view_req has a
function for method="POST" that extracts a "keys" list but I'm not sure
how to set them within the httpd record. 

Erlang rookie here...

Thanks 
Mike

Re: View to find someone in a list in a document

Posted by Jehan Bihin <sq...@gmail.com>.
Thanks guys for your help ! :)


2009/9/2 Simon Metson <si...@googlemail.com>

> Hey Jan,
>
>  function(doc) { for (var v in doc.users) { emit(doc.users[v], doc); }
>>
>> if you leave out the `var` you create a global variable (as opposed to a
>> local variable in JS and that can have funky effects.
>>
>
> What he said.
>
>  function(doc) { if(doc.users) { for (var v in doc.users) {
>> emit(doc.users[v], doc); }}
>>
>> protects you against errors for documents that doesn't have a `users`
>> property.
>>
>
> Yeah, I was going to add that but was being lazy ;) apologies!
> Cheers
> Simon
>

Re: View to find someone in a list in a document

Posted by Simon Metson <si...@googlemail.com>.
Hey Jan,

> function(doc) { for (var v in doc.users) { emit(doc.users[v], doc); }
>
> if you leave out the `var` you create a global variable (as opposed  
> to a local variable in JS and that can have funky effects.

What he said.

> function(doc) { if(doc.users) { for (var v in doc.users)  
> { emit(doc.users[v], doc); }}
>
> protects you against errors for documents that doesn't have a  
> `users` property.

Yeah, I was going to add that but was being lazy ;) apologies!
Cheers
Simon

Re: View to find someone in a list in a document

Posted by Adam Wolff <aw...@gmail.com>.
One more little thing: per spec, for...in enumerates the array keys as
strings.
A

On Thu, Sep 3, 2009 at 8:29 AM, Jesse Hallett <ha...@gmail.com> wrote:

> According to Crockford `for in` gives no guarantee that it will iterate
> over
> array elements in order.  That is in addition to the problem that it will
> also enumerate any attributes on `Array.prototype`.  So Crockford
> recommends
> using `for` with an incrementing index variable instead.
>
> In my opinion Array#forEach is a better option.  But it is a
> Spidermonkey-specific feature, added as part of JavaScript 1.6.  It will
> work fine in CouchDB views, but it will not be available in a lot of web
> browsers.
>
> On Sep 3, 2009 3:46 AM, "Jan Lehnardt" <ja...@apache.org> wrote:
>
> On 3 Sep 2009, at 11:11, Simon Metson wrote: > Hi, >        I'm not sure if
> there's a technical be...
> to get for(... in .. ) right you need to add:
>
> a hasOwnProperty() call to account for prototypical inheritance behaviour.
>
> In addition forEach and a closure can be optimised behind the scenes
> better.
>
> Cheers
> Jan
> --
>
> > Cheers > Simon > > On 3 Sep 2009, at 09:53, Nils Breunese wrote: > >> I
> even learned that u...
>

Re: View to find someone in a list in a document

Posted by Jesse Hallett <ha...@gmail.com>.
According to Crockford `for in` gives no guarantee that it will iterate over
array elements in order.  That is in addition to the problem that it will
also enumerate any attributes on `Array.prototype`.  So Crockford recommends
using `for` with an incrementing index variable instead.

In my opinion Array#forEach is a better option.  But it is a
Spidermonkey-specific feature, added as part of JavaScript 1.6.  It will
work fine in CouchDB views, but it will not be available in a lot of web
browsers.

On Sep 3, 2009 3:46 AM, "Jan Lehnardt" <ja...@apache.org> wrote:

On 3 Sep 2009, at 11:11, Simon Metson wrote: > Hi, >        I'm not sure if
there's a technical be...
to get for(... in .. ) right you need to add:

a hasOwnProperty() call to account for prototypical inheritance behaviour.

In addition forEach and a closure can be optimised behind the scenes better.

Cheers
Jan
--

> Cheers > Simon > > On 3 Sep 2009, at 09:53, Nils Breunese wrote: > >> I
even learned that u...

Re: View to find someone in a list in a document

Posted by Jan Lehnardt <ja...@apache.org>.
On 3 Sep 2009, at 11:11, Simon Metson wrote:

> Hi,
> 	I'm not sure if there's a technical benefit of this syntax, but I  
> think I prefer reading it... Any JS wizards out there like to comment?

to get for(... in .. ) right you need to add:

a hasOwnProperty() call to account for prototypical inheritance  
behaviour.

In addition forEach and a closure can be optimised behind the scenes  
better.

Cheers
Jan
--






> Cheers
> Simon
>
> On 3 Sep 2009, at 09:53, Nils Breunese wrote:
>
>> I even learned that using for (... in ...) is not safe for use with  
>> arrays. I tend to use Array.forEach() [0] like this:
>>
>> ----
>> function(doc) {
>> if (doc.users) {
>>   doc.users.forEach(function(user) {
>>     emit(user, doc);
>>   });
>> }
>> }
>> ----
>>
>> I'm not very proficient in JavaScript, so maybe there is no real in  
>> advantage in the realm of CouchDB.
>>
>> Nils Breunese.
>>
>> [0] https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Objects:Array:forEach
>


Re: View to find someone in a list in a document

Posted by Simon Metson <si...@googlemail.com>.
Hi,
	I'm not sure if there's a technical benefit of this syntax, but I  
think I prefer reading it... Any JS wizards out there like to comment?
Cheers
Simon

On 3 Sep 2009, at 09:53, Nils Breunese wrote:

> I even learned that using for (... in ...) is not safe for use with  
> arrays. I tend to use Array.forEach() [0] like this:
>
> ----
> function(doc) {
>  if (doc.users) {
>    doc.users.forEach(function(user) {
>      emit(user, doc);
>    });
>  }
> }
> ----
>
> I'm not very proficient in JavaScript, so maybe there is no real in  
> advantage in the realm of CouchDB.
>
> Nils Breunese.
>
> [0] https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Objects:Array:forEach

RE: View to find someone in a list in a document

Posted by Nils Breunese <N....@vpro.nl>.
I even learned that using for (... in ...) is not safe for use with arrays. I tend to use Array.forEach() [0] like this:

----
function(doc) {
  if (doc.users) {
    doc.users.forEach(function(user) {
      emit(user, doc);
    });
  }
}
----

I'm not very proficient in JavaScript, so maybe there is no real in advantage in the realm of CouchDB.

Nils Breunese.

[0] https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference:Objects:Array:forEach

________________________________________
Van: Jan Lehnardt [jan@apache.org]
Verzonden: woensdag 2 september 2009 21:54
Aan: user@couchdb.apache.org
Onderwerp: Re: View to find someone in a list in a document

Style police!

On 2 Sep 2009, at 19:00, Simon Metson wrote:

> Hi,
>       Do you mean you want to emit each user as a key? Something like:
>
> function(doc) { for (v in doc.users) { emit(doc.users[v], doc); }

is better written as

function(doc) { for (var v in doc.users) { emit(doc.users[v], doc); }

if you leave out the `var` you create a global variable (as opposed to
a local variable in JS and that can have funky effects.

better yet:

function(doc) { if(doc.users) { for (var v in doc.users) { emit
(doc.users[v], doc); }}

protects you against errors for documents that doesn't have a `users`
property.

otherwise, excellent advice! :)

Cheers
Jan

De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan.

Re: View to find someone in a list in a document

Posted by Jan Lehnardt <ja...@apache.org>.
Style police!

On 2 Sep 2009, at 19:00, Simon Metson wrote:

> Hi,
> 	Do you mean you want to emit each user as a key? Something like:
>
> function(doc) { for (v in doc.users) { emit(doc.users[v], doc); }

is better written as

function(doc) { for (var v in doc.users) { emit(doc.users[v], doc); }

if you leave out the `var` you create a global variable (as opposed to  
a local variable in JS and that can have funky effects.

better yet:

function(doc) { if(doc.users) { for (var v in doc.users) { emit 
(doc.users[v], doc); }}

protects you against errors for documents that doesn't have a `users`  
property.

otherwise, excellent advice! :)

Cheers
Jan
--


>
> as a map would do that.
> Cheers
> Simon
>
> On 2 Sep 2009, at 17:25, Jehan Bihin wrote:
>
>> Hi all,
>>
>> (sorry for my english)
>> I have a document having a list of persons :
>>
>> ...
>> "users":{"membre-joouul_hotmail.com":["createur"],"membre- 
>> killan_daaboo.net
>> ":["moderateur"]}
>> ...
>>
>> Each persons have some roles.
>> My key is the membre like 'membre-joouul_hotmail.com'
>> And i want to list all document having my key present in the users  
>> list.
>>
>> Is it possible and correct to have it ?
>> Can you guide me ? After reading here and the site I found nothing  
>> talking
>> on that.
>>
>> Thanks in advance,
>>
>> Jehan
>


Re: View to find someone in a list in a document

Posted by Simon Metson <si...@googlemail.com>.
Hi,
	Do you mean you want to emit each user as a key? Something like:

function(doc) { for (v in doc.users) { emit(doc.users[v], doc); }

as a map would do that.
Cheers
Simon

On 2 Sep 2009, at 17:25, Jehan Bihin wrote:

> Hi all,
>
> (sorry for my english)
> I have a document having a list of persons :
>
> ...
> "users":{"membre-joouul_hotmail.com":["createur"],"membre- 
> killan_daaboo.net
> ":["moderateur"]}
> ...
>
> Each persons have some roles.
> My key is the membre like 'membre-joouul_hotmail.com'
> And i want to list all document having my key present in the users  
> list.
>
> Is it possible and correct to have it ?
> Can you guide me ? After reading here and the site I found nothing  
> talking
> on that.
>
> Thanks in advance,
>
> Jehan