incubator-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 20:38:14 GMT
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