Document conflict error when updating a design doc

2020-07-18 02:43发布

问题:

I am fetching a design document from "mytest" database on couch DB, but i want to upload the same design document on a different db, lets say "food".

Command which i am using to fetch the design document is :

curl http://localhost:5984/mytest/_design/unique > unique.json

and i get a document which looks like:

{"_id":"_design/unique","_rev":"5-e91630ccf1de8b5784333ea24ce50087","views":{"handle":{"map":"function(doc) {emit(doc.user.id, 1);}","reduce":"function(key, values) {return sum(values);}"},"coordinates":{"map":"function(doc) {\nif((doc.coordinates.coordinates[0] < 145.81) && (doc.coordinates.coordinates[0] > 144.28)\n\t&& (doc.coordinates.coordinates[1] < (-37.47)) && (doc.coordinates.coordinates[1] > (-38.44)))\nemit(doc, 1);\n\n}"}},"language":"javascript"}

but when i am uploading the same design file on food database using :

curl -X PUT http://localhost:5984/food/_design/unique -d @unique.json

I get {"error":"conflict","reason":"Document update conflict."} error. Could you please help me to resolve this error? I am not able to understand he concept of revision numbers and how we can actually put this file onto the database.

Thank you!!

回答1:

When you are updating a document, you have to provide the the revision of the document you are updating. It allows to only update the latest version of the document and will fail if someone updated the document since you last saw (fetched) it. In your case you should first execute a HEAD http request to the food database design doc you are trying to update. HEAD is fairly lightweight and will return basic info about a document, including its current revision (ETag field in the response). Then use that revision to update the doc with new content. More info about the API: https://docs.couchdb.org/en/stable/api/document/common.html