ethilesen
ethilesen

Reputation: 19

Cloudant - partly update document - http api

I have a need to partly update a document in a cloudant db. The reason is that I want to create a doc, add a image (attachment) to that doc and send the img url to an external image processor. Later I want to update the doc in cloundant with the result of the image processing. I'm doing this from swift/mobile and don't want to copy the file more than once. There seems to be no way to partly update a doc, according to the api doc. But maybe someone has found a way to do this. I see that I can copy docs, but assume that will give me the same isse when I need to add some info to the doc. Maybe it's possible to copy and merge?

Upvotes: 0

Views: 817

Answers (2)

Nuno Cruces
Nuno Cruces

Reputation: 1743

If the question is about updating docs independently of attachments, this is not only possible, but recommended.

First PUT the document, then PUT the attachment (with the appropriate rev query parameter). Afterwards, you can update the document independently from the attachment.

To update the document (without updating the attachment), you should GET the document, and note that it includes an "attachment stub". As long as you preserve the stub when you PUT a new version of the document, the attachment will be preserved. Example of a document with an attachment stub:

{
  "_id":"attachment_doc",
  "_rev":1589456116,
  "_attachments":
  {
    "foo.txt":
    {
      "stub":true,
      "content_type":"text\/plain",
      "length":29
    }
  }
}

Answering your more general question about partial updates, you can use update functions, but they are more cumbersome when working with attachments.

Upvotes: 0

bradnoble
bradnoble

Reputation: 75

The trick is to query Cloudant for the doc (all of the JSON), add or update the key/value pair of the field in question, and then save the doc (or, PUT the doc) to the database.

Make sure that when you save the doc, you include the latest _rev value.

More here: http://docs.cloudant.com/document.html#update

Upvotes: 0

Related Questions