Count query in CouchDB

20 Views Asked by At

I have a question about Coucb DB.

I have a bunch of documents that looks like json below:

{
  "_id": "some-id",
  "_rev": "some-rev",
  "text": "some text",
  "createdBy": {
    "userKey": "some-user-key",
  },
  "relatedTo": {
    "relatedToId": "some-related-to-id",
  },
  "readBy": [
    {
      "userKey": "another-user-key"
    }
  ],
  "createdAt": "1970-01-01T00:00:00.000+00:00"
}

I need to count documents that match Mango query below:

"selector": {
    "$and": [
      {
        "createdBy.userKey": {
          "$ne": "not-equals-user-key"
        }
      },
      {
        "$or": [
          {
            "readBy": {
              "$not": {
                "$elemMatch": {
                  "userKey": {
                    "$eq": "not-equals-read-by-user-key"
                  }
                }
              }
            }
          },
          {
            "readBy": {
              "$exists": false
            }
          }
        ]
      },
      {
        "relatedTo.relatedToId": {
          "$in": [
            "related-to-id-1",
            "related-to-id-2,
            "related-to-id-N"

          ]
        }
      }
    ]
  }

My questions:

  1. Is there a way to count number of docs that match above Mango query effictiently?
  2. Can I make Index that match that query to improve performance ( and how that index will looks like ( my current obvious indexes ( for all fields in query ) are not working ) )
  3. Can I use 'View' to make such filtering in such not very plain object?

When I try to use '_find' API with above Mango query to look up collection of 50k records ( not very much ) I got about 10 seconds to execute.

0

There are 0 best solutions below