1

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ben
  • 4,486
  • 6
  • 33
  • 48

1 Answers1

2

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • Right now I am experimenting with different designs. I have multiple binary data files beloning to one document (around 10) and the advantage of the design I described above is, that deleting a document can be done in one request. If my document only contains references to the binary files, I have to delete those separately in multiple requests, first. But being able to bulk process a subset of attachments is quite important for performance reasons, so I guess I will have to find another way. – Ben Feb 20 '20 at 10:02
  • Another option, which might work for thumbnails, is to include the thumbnails as Base64 encoded data in the document directly. That has other drawbacks, such as not being able to easily _exclude_ them. But if they're small enough, it might be a reasonable approach. – Jonathan Hall Feb 20 '20 at 10:33