使用 reduce functions/linked 文档的 Couchdb 过滤器

Couchdb filter using reduce functions/linked documents

正在考虑: 文档简介 { _id:"1", name:"john", likes: ["2222","1111"] }

医生喜欢 { _id:"2222", value:"true" }

{ _id:"1111", value:"false" }

我的 xamarin 应用程序上有一个过滤器来获取配置文件,它运行良好,但我需要包含 "children"(链接的)文档...我可以通过视图设置来做到这一点 include_docs=true 但我希望 couchdb 进行过滤,以便我可以使用复制。 另外,如果我可以使用 reduce 函数来过滤数据,也可以实现相同的结果,但我不能让过滤器使用 reduce 函数。所以,有什么想法吗?

预期结果为:

文档简介 { _id:"1", name:"john", likes: { {_id:"2222", value:"true"}, {_id:"1111", value:"false"] } }

谢谢!

I can do this with a view setting include_docs=true but I want couchdb to filter so I can use replication

您可能已经知道这一点,但您可以使用 couchdb views as filters

Also, it would be possible to accomplish the same result if I could use a reduce function to filter data

reduce 函数用于 "reducing" 由 map 函数 return 编辑的值。映射函数 return 是一个键和一个值,如下所示:

emit(key,value)

reduce 函数仅获取从 map 函数 returned 的键和值。例如,如果您使用

调用视图

?key=abc

它 return 的结果类似于

[{
_id:...,
type: abc
},
{
_id:...,
type:abc
}
....
]

您已经通过关键字 "abc" 过滤了所有文档。 reduce 函数将获取键、值和 rereduce 参数作为输入。如果将 reduce 函数用作 post 地图处理步骤以进一步过滤视图中的结果,则会出现两个问题:

  1. 无法将参数传递给 reduce。您指定的键将仅由 map 函数使用,然后按原样传递给 reduce。

  2. 反正也不是什么好主意。使用 reduce 你想要 return 一个小的值来聚合你从视图中获得的结果。因此,以上面的示例为例,如果您 return 说一个整数作为 map 函数 ( in emit(key,value)//suppose that the value is an integer) 的值,则 reduce 函数可能 return 这些值的总和或聚合。但是尝试 return 修改后的文档并不是 reduce 函数的目的。来自 the docs

"A reduce function must reduce the input values to a smaller output value. If you are building a composite return structure in your reduce, or only transforming the values field, rather than summarizing it, you might be misusing this feature. "

List functions 可能更适合您的尝试。如果你想在 returning 之前处理视图查询的结果,他们就可以了。

在列表函数中,您会得到一组由视图函数 return 编辑的结果。如果您想对它们应用复杂的过滤器,您甚至可以传递额外的参数。但是您将无法使用列表函数进行复制。

最后,复制在文档级别进行。文档具有 _rev 字段,复制器进程使用该字段在执行复制之前检查文档的版本。因此,您将无法复制视图 return 编辑的结果。只会复制文档。