couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Markus Jelsma" <mar...@buyways.nl>
Subject Re: view syntax for retrieving object without a field
Date Fri, 22 Jan 2010 21:25:54 GMT
Hello Adam,


Thanks for your suggestion, the "undefined" indeed doesn't work as
expected. Using undefined (without quotes) does, the same goes for using
=== void 0.

I updated the wiki with your suggestion, thanks!


Cheers,

Adam Wolff said:
> In your first example, you wrote
>
>   if (doc.field == "undefined")
>
>
> I think you meant
>
>   if (typeof doc.field == "undefined")
>
>
> But I usually write
>
>   if (doc.field === void 0)
>
> if I really want to check for undefined, since "undefined" is not a js
> keyword; it's just a conventional variable that has no value.
>
> A
>
> On Fri, Jan 22, 2010 at 12:38 PM, Markus Jelsma <markus@buyways.nl>
> wrote:
>
>> Hello,
>>
>>
>> For the sake of clarity and reusability i have added this problem to
>> the wiki [1]. Please check and modify or extend if there are some
>> flaws or if something is missing.
>>
>> [1]:
>> http://wiki.apache.org/couchdb/View_Snippets#documents_without_a_field
>>
>>
>> Cheers,
>>
>>
>> Markus Jelsma said:
>> > Hi,
>> >
>> >
>> > I have some documents, all with a `name` field and some with a
>> country field  defined. Using the map function below i can retrieve
>> all documents that have  no country field defined.
>> >
>> > function (doc)
>> > {
>> >   var
>> >
>> >   if (doc.country == "undefined")
>> >   {
>> >     emit(doc.name, null);
>> >   }
>> > }
>> >
>> > I do believe this could be very unusable in many situations,
>> especially if you  need to retrieve documents based on the absence
>> of a lot of different fields.
>> >
>> > Let's provide you with a better alternative, you need only one view
>> to handle  all possible negations, you only need to specify on which
>> negations you want  to select, check it out!
>> >
>> > function (doc)
>> > {
>> >   var fields = new Array("name", "country");
>> >
>> >   for (idx in fields)
>> >   {
>> >     if (typeof eval("doc." + fields[idx]) == "undefined")
>> >     {
>> >       emit(fields[idx], null);
>> >     }
>> >   }
>> > }
>> >
>> > Now you can just query the view with your key and retrieve all
>> documents
>> >  without that field.
>> >
>> > Hope this helps.
>> >
>> >
>> > Cheers,




Mime
View raw message