This is the approach I'd recommend as well. A very common CouchDB pattern is read-modify-write. So, you'd read the document + all of its attachments into memory of your process, modify it to be the new version you want, then write the new document back, adding its attachments afterwards.
Remember that the granularity of a "transaction" in CouchDB is a *single* document. There is no operation that operates on 2 or more documents at once. Renaming a document is effectively a DELETE and a PUT/POST at the same time (ignoring attachments), so rename at present does not exist as a single operation. If you don't want to have to perform multiple PUT/POST operations to create a document with attachments, I believe there is a way to pre-encode those attachments and write the entire thing in one go. The exact approach eludes me right now, though....something something multipart request. -Joan ----- Original Message ----- > From: "Bill Stephenson" <[email protected]> > To: [email protected] > Sent: Thursday, September 27, 2018 4:11:37 PM > Subject: Re: Updating _id for documents with attachment in CouchDB > > I may have missed something but can’t you just load the data from the > old doc, and the additional data, and any changes into a new doc and > save that, and delete the old on if necessary? > > I may be wrong, but I assume by design you can’t change the _id of an > existing doc and “save” it. > > Kindest Regards, > > Bill > > > > > On Sep 27, 2018, at 1:34 PM, Arcadius Ahouansou > > <[email protected]> wrote: > > > > Hello. > > I have a set of documents with _id in the format "doc-1", ..., > > "doc-N" > > shown below. > > I need to change the _id of those documents from let's say "doc-1" > > to > > "user-1" and also add some additional fields to the documents. > > > > To prove my strategy, I started by loading only 1 document, then > > updating > > the _id, add additional fields, remove the _rev field and save it. > > This fails with error: > > > > HTTP 412 Precondition Failed > > > > {"error":"missing_stub","reason":"Invalid attachment stub in user-1 > > for > > photo.png"} > > > > when I remove the stub:true, I get > > > > {"error":"bad_request","reason":"Invalid attachment data for > > photo.png"} > > > > > > Is there a way to link this new document to the existing attachment > > without > > having to fully load and copy the attachment? > > > > I have noticed that there is also a COPY API available for CouchDB > > which is > > able to copy a document preserving all attachments. > > My only issue with the COPY is that it seems to operate only on a > > single > > document and there seem to be no way of modifying the document > > being > > copied. > > In my case, I need to be able to perform copy and multiple > > documents at > > once. > > > > Any hint will be very appreciated. > > > > Thank you very much. > > > > Arcadius. > > > > > > { > > "_id": "doc-001", > > "_rev": "3-61954f961abfb012ff7262ebb2d40426", > > "_attachments": { > > "photo.png": { > > "content_type": "image/png", > > "revpos": 3, > > "digest": "md5-IfggOdAm507L7pnXzfCzWA==", > > "length": 6873, > > "stub": true > > } > > } > > } > >
