user977828
user977828

Reputation: 7679

How to query for an element in an array

The following documents are stored in CouchDB

{
  {
    "_id": "1",
    "_rev": "3-2882a4696d580cdef754a93c7e5e77e7",
    "comb": [
        "4-5",
        "4-6",
        "5-6"
    ]
  },
  {
    "_id": "2",
    "_rev": "2-1991a898a5457308e3a89253e695cef5",
    "comb": [
        "4-5",
        "5-6"
    ]
  }
}

Below is the map function

function(doc) {
  emit(doc.comb, null);
} 

and http://localhost:5984/comb/_design/snp/_view/by_test?key="4-6" returns only

{"total_rows":2,"offset":0,"rows":[

]}

How is it possible to find an element in an array?

Upvotes: 1

Views: 62

Answers (1)

Kim Stebel
Kim Stebel

Reputation: 42047

You would need to call emit for each element of the array:

for (var i in doc.comb) {
    emit(doc.comb[i], null);
}

Then your query will find all documents that have a comb array which includes the key specified in the query.

Upvotes: 2

Related Questions