CouchDB 中 $elemMatch 的索引

Index for $elemMatch in CouchDB

我的 CouchDB 文档包含一个数组字段,一个简化的示例:

{
    "organizations": [
        { "id": "Org 1" },
        { "id": "Org 2" }
    ]
}

我想使用以下查询查找包含组织 Org 1 的所有文档:

{
    "selector": {"$elemMatch": {"id": "Org 1"}}
}

有没有办法为它使用索引?我试过了

{
    "fields": ["organizations.[].id"]
}

这似乎没有任何效果,我收到相同的 "no matching index found" 警告。

使用基于 JSON/view 的标准索引,您无法为 $elemmatch 构建有用的索引,请参阅 Couchdb documentation for json index definition is unclear on support for indexing array fields #1388

虽然您可以为整个数组或逐个元素编制索引,但都不会为与任何数组位置的元素匹配的文档生成连续的键列表。

如果您使用来自 cloudant 的插件构建 couchdb,您可以使用支持 $elemmatch 但具有不同特性的文本索引。我建议查看 this article and other links in .