Search code examples
couchdbpouchdbemit

Map Function with multiple keys


I am running a couchDB Server with the following map function:

function(doc) {
            if (doc.data.appVersion)  {
                emit([doc.data.appVersion, doc.data.date], doc);
            }
        }

I can easily query for a appVersion or a range of appVersion by using something like this:

Start key: ["1.2.3", 0] End key: ["2.0.0", 10000000000000]

I can also query for a appVersion between two dates:

Start key: ["1.2.3", 1420066800000] End key: ["1.2.3", 1422572400000]

What I can't do is to get all docs regardless of their appVersion but between two dates. This does not work:

Start key: ["0.0.0", 1420066800000] End key: ["9.9.9", 1422572400000]

The query would also include all docs with an appVersion between 0.0.0 and 9.9.9. The date is completely ignored. It is like:

"get all docs with appVersion between 0.0.0 and 9.9.9 OR date between 1420066800000 and 1422572400000".

But want I want is:

"get all docs with appVersion between 0.0.0 and 9.9.9 AND date between 1420066800000 and 1422572400000"

Is there a way to do all make all three query work? Do I have to change the map-function? How? Or do I have to create another map function just for that last kind of query. I'd like to do this with one map-function because there are more properties I'd like to query for.

I am using couchDB with pouchDB on the client side.


Solution

  • Unfortunately, you do need to create a separate design document that emits only the doc.data.date.

    Another tip: there is no need to emit(doc.myKey, doc). You can just do emit(doc.myKey) and then use {include_docs: true} when you query(). This saves space under the hood; there are details in this blog post.