查找缺少任意字段的CouchDB文档 [英] Find CouchDB docs missing an arbitrary field

查看:72
本文介绍了查找缺少任意字段的CouchDB文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个CouchDB视图,其中我可以找回没有任意字段的所有文档。如果您事先知道文档可能没有的字段,这很容易做到。 例如,您可以发送 view / my_view /?键=foo以便轻松检索没有foo字段的文档:

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, 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);
    }
  }
}

询问视图中设置的三个字段;像 view / my_view /?key =baz不会给你任何东西,即使你有很多文档缺少该字段。我需要一个视图,它会在哪里,我不需要提前指定可能缺少的字段。任何想法?

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 that field. I need a view where it will--where I don't need to specify possible missing fields in advance. Any thoughts?

推荐答案

这种技巧称为泰式按摩

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;
}

要查找没有该字段的文档,

To find documents not having that field,


  1. GET / db / _all_docs 并记住所有ids

  2. GET / db / _design / ex / _view / fields?reduce = false& key =some_field

  3. $ c> _all_docs 与查询中的ID。

  1. GET /db/_all_docs and remember all the ids
  2. GET /db/_design/ex/_view/fields?reduce=false&key="some_field"
  3. Compare the ids from _all_docs vs the ids from the query.

_all_docs 但不在视图中是那些缺少该字段。

The ids in _all_docs but not in the view are those missing that field.

这是不好的保持id在内存中,但你没有至!您可以使用合并排序策略,同时迭代两个查询。您以列表(从视图)的第一个ID和完整列表(从_all_docs)的第一个ID开头。

It sounds bad to keep the ids in memory, but you don't have to! You can use a merge sort strategy, iterating through both queries simultaneously. You start with the first id of the has list (from the view) and the first id of the full list (from _all_docs).


  1. 如果已满 元素

  2. 如果已满 em> 元素

  3. 如果全部> 元素

  1. If full < has, it is missing the field, redo with the next full element
  2. If full = has, it has the field, redo with the next full element
  3. If full > has, redo with the next has element

取决于您的语言,难。但是,在JavaScript中,例如,或其他事件驱动的编程框架,这很容易。

Depending on your language, that might be difficult. But it is pretty easy in Javascript, for example, or other event-driven programming frameworks.

这篇关于查找缺少任意字段的CouchDB文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆