You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Arcadius Ahouansou <ar...@menelic.com> on 2018/09/27 18:34:00 UTC

Updating _id for documents with attachment in CouchDB

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
    }
  }
}

Re: Updating _id for documents with attachment in CouchDB

Posted by Joan Touzet <wo...@apache.org>.
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" <bi...@cherrypc.com.INVALID>
> To: user@couchdb.apache.org
> 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
> > <ar...@menelic.com> 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
> >    }
> >  }
> > }
> 
> 

Re: Updating _id for documents with attachment in CouchDB

Posted by Jan Lehnardt <ja...@apache.org>.

> On 27. Sep 2018, at 22:11, Bill Stephenson <bi...@cherrypc.com.INVALID> wrote:
> 
> 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 assume downloading and uploading the attachments is prohibitive.

If that’s the case, and the atomicity issues that Joan outlined are not a problem, I’d recommend a doc-by-doc COPY and subsequent read/write of just the JSON to add your fields.

Best,
Jan
—

> 
> 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 <ar...@menelic.com> 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
>>   }
>> }
>> }
> 

-- 
Professional Support for Apache CouchDB:
https://neighbourhood.ie/couchdb-support/


Re: Updating _id for documents with attachment in CouchDB

Posted by Bill Stephenson <bi...@cherrypc.com.INVALID>.
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 <ar...@menelic.com> 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
>    }
>  }
> }