couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Couchdb Wiki] Trivial Update of "View_Snippets" by MarkusJelsma
Date Fri, 22 Jan 2010 20:34:52 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The "View_Snippets" page has been changed by MarkusJelsma.
The comment on this change is: added Retrieving documents without a certain field.
http://wiki.apache.org/couchdb/View_Snippets?action=diff&rev1=30&rev2=31

--------------------------------------------------

    * [[#standard_deviation|Computing the standard deviation]]
    * [[#summary_stats|Computing simple summary statistics (min,max,mean,standard deviation)
]]
    * [[#interactive_couchdb|Interactive CouchDB Tutorial]]
+   * [[#documents_without_a_field|Retrieving documents without a certain field]]
  
  
  <<Anchor(common_mistakes)>>
@@ -667, +668 @@

  == Interactive CouchDB Tutorial ==
  See [[http://labs.mudynamics.com/2009/04/03/interactive-couchdb/|this blog post]], which
is a CouchDB emulator (in JavaScript) that explains the basics of map/reduce, view collation
and querying CouchDB RESTfully.
  
+ <<Anchor(documents_without_a_field)>>
+ == Retrieving documents without a certain field ==
+ Sometimes you might need to get a list of documents that '''don't''' have a certain field.
You can do this quite easy by emitting keys that fit the "undefined" condition:
+ 
+ {{{
+ map
+ function(doc)
+ {
+   if (doc.field == "undefined")
+   {
+     emit(doc.id, null);
+   }
+ }
+ }}}
+ 
+ However, if you have more than just a few fields that need to be tested for abcense you
can use another approach instead of creating a view for each negation:
+ 
+ {{{
+ function (doc)
+ {
+   // List of fields to test for abcense in documents, fields specified here will be emitted
as key
+   var fields = new Array("type", "role", "etc");
+ 
+   // Loop through our fields
+   for (idx in fields)
+   {
+     // Does the current field exists?
+     if (typeof eval("doc." + fields[idx]) == "undefined")
+     {
+       // It doesn't, emit the field name as key
+       emit(fields[idx], null);
+     }
+   }
+ }
+ }}}
+ 
+ For example: yuu can now query your view and retrieve all documents that do not contain
the field `role` (view/NAME/?key="role").
+ 

Mime
View raw message