How do you delete a couchdb document with an empty “” document id?

依然范特西╮ 提交于 2019-12-23 12:26:01

问题


I see the document in the db as this: {_id: "", _rev: "1-2f11e026763c10730d8b19ba5dce7565", forbidden: "must supply latest _rev to update existing package"}

Everything I see in the docs shows referring to the document with the ID, but of course this can't happen.

I'm not exactly sure how this happened, most likely something faulty between the chair and keyboard, but I wanted to know if there was a possibility of fixing this without starting over.


回答1:


There is a bug in CouchDB that allows this to happen. I believe it was introduced in v1.1.0 and will be fixed in v1.1.1 and v1.2.0.

The bug is that _update functions can create documents with empty-string IDs. To delete the document, use the same update function and exploit the same bug.

For example:

{ "_id": "_design/example",
  "updates": {
    "del_blank":
    "function(doc, req) {
       var doc = {_id:'', _rev:req.query.rev, _deleted:true};
       return [doc, 'Trying to delete nastydoc@'+doc._rev];
     }"
   }
}

Simply provide the revision that is giving you problems and it will mark it deleted.

$ rev="1-2f11e026763c10730d8b19ba5dce7565"
$ curl -XPOST localhost:5984/db/_design/example/_update/del_blank?rev=$rev
Trying to delete nastydoc@1-2f11e026763c10730d8b19ba5dce7565

$ curl localhost:5984/db/_all_docs
{"total_rows":1,"offset":0,"rows":[
{"id":"_design/example","key":"_design/test","value":{"rev":"2-b9bfbedff0c09fab88ff36d06cec0d34"}},
]}



回答2:


Concerning version 2.1.1:

There is a bug in Project Fauxton that allows you to clone a document while leaving the new ID blank. Neither the Project Fauxton nor CouchDB's API offers a way to edit or delete documents with blank IDs.

In JasonSmith's answer, the del_blank update function throws {"error":"illegal_docid","reason":"Document id must not be empty"}. So, you must replicate the database into a new database, which will store documents with non-empty IDs.



来源:https://stackoverflow.com/questions/7604557/how-do-you-delete-a-couchdb-document-with-an-empty-document-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!