1

My CouchDB documents contain an array field, a simplified example:

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

I want to find all the documents that include the organization Org 1, using this query:

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

Is there a way to use an index for it? I tried

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

which does not seem to have any effect, I receive the same "no matching index found" warning.

bereal
  • 32,519
  • 6
  • 58
  • 104

1 Answers1

1

With the standard JSON/view based indexes you can not build a useful index for $elemmatch, see Couchdb documentation for json index definition is unclear on support for indexing array fields #1388.

While you can index the full array or element by element, neither is generating a contiguous list of keys for docs that match on an element in any array position.

If you build couchdb with plugins from cloudant you can use text indexes which do have support for $elemmatch but have different characteristics. I would recommend looking at this article and other links in this past question.

lossleader
  • 13,182
  • 2
  • 31
  • 45