根据嵌套子文档中的值过滤 couchdb 文档
Filter couchdb document based on value from nested child document
我想创建一个 map/reduce 函数来根据子文档中的嵌套值过滤文档。但是检索父文档。
我有以下文件:
{
"_id": "1",
"_rev": "1-991baf1d86435a73a3460335cc19063c",
"configuration_id": "225f9d47-841c-43c2-90c2-e65bb49083d3",
"name": "test",
"image": "",
"type": "A",
"created": "",
"updated": 1,
"destroyed": ""
}
{
"_id": "225f9d47-841c-43c2-90c2-e65bb49083d3",
"_rev": "1-3e3a1c357c86cbd1cd42b5980b9655a4",
"configuration_packages_id": "cd19b0ba-157d-4dd4-adac-56fd470bfed4",
"configuration_distribution_id": "5b538411-ca99-46c7-ac3c-1f382e4577a9",
"type": "CONFIGURATION",
"configuration": {
"hostname": "example123",
"images": [
"image1",
"image2"
]
}
}
现在我想检索类型 A 和主机名 example123 的所有文档。
目前我像这样检索所有类型 A 的文档:
function (doc) {
if (doc.type === "A") {
emit([doc.updated], doc);
}
}
但现在我也想过滤主机名。 我不确定如何使用 CouchDB 实现此目的。
TLDR;
你不能这样做
详情
您的 "nested" 文档只能通过联接访问,但您不能查询它。
本机执行此类查询的正确方法是在父文档中包含一个真正的嵌套文档。分离这些文件是有代价的。
加入示例
function (doc) {
if (doc.type === "A") {
emit([doc.updated,0]);
emit([doc.updated,1],["_id":doc.configuration_id]);
}
}
如果您使用 "include_docs=true" 查询视图,这将使您获得链接的配置文档以及父文档本身。然后您可以查询以获取更新的文档,将 nested(1) 与 parents(0) 合并并过滤它们。