Multiple key search in CouchDB

couchdb

Solution

Emit both keys (or only one if equal):

function(doc) {
  if (doc.hasOwnProperty('key1')) {
    emit(doc.key1, 1);
  }
  if (doc.hasOwnProperty('key2') && doc.key1 !== doc.key2) {
    emit(doc.key2, 1);
  }
}

Query with (properly url-encoded):

?include_docs=true&key=123

or with multiple values:

?include_docs=true&keys=[123,567,...]

UPDATE: updated to query multiple values with a single query.

Problem

Given the following object structure: ``` { key1: "...", key2: "...", data: "..." } ``` Is there any way to get this object from a CouchDB by quering both key1 and key2 without setting up two different views (one for each key) like: ``` select * from ... where key1=123 or key2=123 ``` Kind regards, Artjom edit: Here is a better description of the problem: The object described above is a serialized game state. A game has exactly one creator user (key1) and his opponent (key2). For a given user I would like to get all games where he is involved (both as creator and opponent).

Original source