Find CouchDB documents without any field

I need a CouchDB view where I can return all documents that don't have an arbitrary field. This is easy to do if you know in advance which fields the document may be missing. For example, this allows you to send view/my_view/?key="foo"to easily receive documents 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 are limited to the three fields set in the view; something like view/my_view/?key="baz"this will not give you anything, even if you have a lot of documents that do not have this field. I need to see where it will be - where I do not need to indicate possible missing fields in advance. Any thoughts?

+3
source share
2 answers

. , ( , ) .

function(doc) {
    // _view/fields map, showing all fields of all docs
    // In principle you could emit e.g. "foo.bar.baz"
    // for nested objects. Obviously I do not.
    for (var field in doc)
        emit(field, doc._id);
}

function(keys, vals, is_rerun) {
    // _view/fields reduce; could also be the string "_count"
    return re ? sum(vals) : vals.length;
}

, ,

  • GET /db/_all_docs
  • GET /db/_design/ex/_view/fields?reduce=false&key="some_field"
  • _all_docs .

_all_docs, , - , .

, , ! , . ( ) ( _all_docs).

  • < , ,
  • full = has, , redo
  • full > has, redo

. Javascript, , , .

+2

, . , . .

, . , , .

+1

All Articles