couchdbattachmentcouchdb-mango

How to fetch subset of attachments


I have a CouchDB with documents, which look like this:

{
  "_id": "000040cc-e3b4-47cc-b051-a5508efb8996",
  "_rev": "1-882d7f88cc2e1e767b55d0c82fb638d2",
  "state": "uploaded",
  "state_since": "2020-02-17T11:20:55.1450252Z"
  // more metadata ...

  "_attachments": {
    "large.jpg": {
      "content_type": "image/jpeg",
      "revpos": 1,
      "digest": "md5-NK7ejYjrErhMAs7tZ4+R8w==",
      "length": 87846,
      "stub": true
    },
     "medium.jpg": {
      ...
    },
    "small.jpg": {
      ...
    }
  }
}

Let's assume, I want to query a set of images like this:

{
   "selector": {
      "state": "uploaded"
   },
   "sort": ["state_since"],
   "limit": 100
}

If I want to display the thumbnails of those 100 images, I'd have to iterate through the result list and download the corresponding attachments. This would be 101 requests in total.

I could also do it in one request by specifying, that I want to fetch the documents with attachments. But this would return all (potentially very large) attachments.

I know that I can set the fields property in my query to only return the fields I need. But can I apply this to attachments, too? And if yes: how?


Solution

  • No, there's no way to do what you're requesting. The only ways to fetch a subset of attachments are by fetching them one at a time, or by using the atts_since attribute when fetching a single document, which is intended for use in replication.

    Perhaps consider re-designing your documents. Perhaps you can store your thumbnails on a separate document, that only contains thumbnails.