You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Jason Priem <pr...@email.unc.edu> on 2011/05/31 23:03:44 UTC

Find CouchDB docs missing an arbitrary field

Hi all,
I need a CouchDB view where I can get back all the documents that don't 
have an arbitrary field. This is easy to do if you know in advance what 
fields a document *might* not have. For example[1], this lets you send 
view/my_view/?key="foo" to easily retrieve docs without the "foo" field:

function (doc) {
   var fields = [ "foo", "bar", "etc" ];

   for (var idx in fields) {
     if (!doc.hasOwnProperty(fields[idx])) {
       emit(fields[idx], 1);
     }
   }
}

However, you're limited to asking about the three fields set in the 
view; something like view/my_view/?key="baz" won't get you anything, 
even if you have many docs missing field baz. I need a view where it 
will--where I don't need to specify possible missing fields in advance. 
Any thoughts?

[1] 
http://wiki.apache.org/couchdb/View_Snippets#Retrieving_documents_without_a_certain_field

(apologies for crosspost at 
http://stackoverflow.com/questions/6183352/find-couchdb-docs-missing-an-arbitrary-field) 


-- 
Jason Priem
UNC Royster Fellow
School of  Information and Library Science
University of North Carolina at Chapel Hill

Re: Find CouchDB docs missing an arbitrary field

Posted by Gabor Ratky <rg...@rgabostyle.com>.
You can't do this in a traditional CouchDB view unless you have a set list of fields you're checking for. Unless you want to emit all possible field names that don't exist ;)

I would probably use couchdb-lucene [1], and index all participating documents with a given term (e.g. a type field in the documents) along with the names of the field THAT EXIST. You will then be able to search for "type:foo -field:baz" [2] in lucene query syntax and get back all documents that are type:foo but do not contain 'baz'.

Gabor

[1] https://github.com/rnewson/couchdb-lucene
[2] In the example, the type of the document was added under the field 'type' into the lucene index, where the names of the field as 'field' field. See the indexing options in [1].
On Tuesday, May 31, 2011 at 11:03 PM, Jason Priem wrote:
> Hi all,
> I need a CouchDB view where I can get back all the documents that don't 
> have an arbitrary field. This is easy to do if you know in advance what 
> fields a document *might* not have. For example[1], this lets you send 
> view/my_view/?key="foo" to easily retrieve docs without the "foo" field:
> 
> function (doc) {
>  var fields = [ "foo", "bar", "etc" ];
> 
>  for (var idx in fields) {
>  if (!doc.hasOwnProperty(fields[idx])) {
>  emit(fields[idx], 1);
>  }
>  }
> }
> 
> However, you're limited to asking about the three fields set in the 
> view; something like view/my_view/?key="baz" won't get you anything, 
> even if you have many docs missing field baz. I need a view where it 
> will--where I don't need to specify possible missing fields in advance. 
> Any thoughts?
> 
> [1] 
> http://wiki.apache.org/couchdb/View_Snippets#Retrieving_documents_without_a_certain_field
> 
> (apologies for crosspost at 
> http://stackoverflow.com/questions/6183352/find-couchdb-docs-missing-an-arbitrary-field) 
> 
> 
> -- 
> Jason Priem
> UNC Royster Fellow
> School of Information and Library Science
> University of North Carolina at Chapel Hill