CouchDB _changes-API过滤器

时间:2018-02-05 16:07:38

标签: filter couchdb

我试图在CouchDB 2.1.1上使用已过滤的_changes-API流来工作,但我遇到了问题。 我只想通过包含字段" type"的更改Feed来接收文档。价值"文章"。

根据documentation这样的事情应该有效:

function (doc, req) {
  if (doc.type && doc.type == 'article') {
    return true;
  }
  return false;
}

我使用Fauxton在名为type_article的_design文档中的名为filters的新视图中创建了上述函数。当我点击视图时,我看不到任何结果。

现在我想使用过滤器作为GET参数从数据库中检索过滤的更改Feed: localhost:5984/my_database/_changes?filter=filters/type_article

CouchDB的反应是 {"error":"not_found","reason":"missing json key: filters"}

您是否了解如何使过滤器功能正常工作?

PS:我也尝试过使用' emit()'函数而不是返回truefalse,这会返回预期的结果,但在尝试查询_changes时会出现相同的错误。

2 个答案:

答案 0 :(得分:1)

只需注意,您实际上可以将视图用作_changes的过滤器。在这种情况下,如果地图函数为其发出至少一条记录,则会计算一个文件。

例如,如果设计文档(称为“ddoc”)看起来像这样:

{"views":
    {"type_article":
        {"map":
            "function(doc) {
                if (doc.type && doc.type == 'article') {
                    emit(doc.name);
                }
            }"
        }
    }
}

然后查询看起来像localhost:5984/my_database/_changes?filter=_view&view=ddoc/type_article。请注意缺少_design前缀和属性_view的关键字filter。以下是相应文档部分的链接:[link]

这里唯一需要注意的是,这个过滤器实际上并没有使用构建的视图索引,因此不会比普通的过滤函数更快。

答案 1 :(得分:0)

我发现了问题。使用#Structure of the matrix row.names V1 V2 V3 1 22936 22936 3134 1222 2 285855 285855 5123 1184 3 10409 10409 2857 1142 4 6556 6556 1802 1089 #Get values from the matrix z=the_matrix['22936',3] #z equals 1222 z_prime=the_matrix['rowname_not_inmatrix',3] #returns an error: #Error in the_matrix["rowname_not_inmatrix", 3] : subscript out of bounds #(z_prime remains not defined) 菜单条目旁边的小+号在Fauxton中创建视图时,您只能创建视图。视图与过滤器不同。 要创建适用于_changes Feed的过滤器,请点击“创建文档”。并创建一个这样的文档:

Design Documents

这将创建一个名为{ "_id": "_design/filters", "filters": { "type_article": "function (doc, req) {\n if (doc.type && doc.type == \"article\") {\n return true;\n } else { \n return false; \n}\n}" } } 的新设计文档,其函数为filters