开发者

Can I retrieve all revisions of a deleted document?

I know I can retrieve all revisions of an "available" document, but can I retrieve the last "available" version of a deleted document? I do not know the revision id prior to the delete. This is the command I am currently running...it return开发者_如何学JAVAs {"error":"not_found","reason":"deleted"}.

curl -X GET http://localhost:5984/test_database/a213ccad?revs_info=true


I've got this problem, trying to recover deleted document, here is my solution:

0) until you run a compaction, get deleted history, e.g.:

curl http://example.iriscouch.com/test/_changes

1) you'll see deleted documents with $id and $rev, put empty document as new version, e.g.:

curl -X PUT http://example.iriscouch.com/test/$id?rev=$rev -H "Content-Type: application/json" -d {}

2) now you can get all revisions info, e.g:

curl http://example.iriscouch.com/test/$id?revs_info=true

See also Retrieve just deleted document


Besides _changes, another good way to do this is to use keys with _all_docs:

GET $MYDB/_all_docs?keys=["foo"] ->

{
    "offset": 0,
    "rows": [
        {
            "id": "foo",
            "key": "foo",
            "value": {
                "deleted": true,
                "rev": "2-eec205a9d413992850a6e32678485900"
            }
        }
    ],
    "total_rows": 0
}

Note that it has to be keys; key will not work, because only keys returns info for deleted docs.


You can get the last revision of a deleted document, however first you must first determine its revision id. To do that, you can query the _changes feed and scan for the document's deletion record — this will contain the last revision and you can then fetch it using docid?rev=N-XXXXX.

I remember some mailinglist discussion of making this easier (as doing a full scan of the changes feed is obviously not ideal for routine usage), but I'm not sure anything came of it.


I've hit this several times recently, so for anyone else wandering by ...

This question typically results from a programming model that needs to know which document was deleted. Since user keys such as 'type' don't survive deletion and _id is best assigned by couch, it would often be nice to peak under the covers and see something about the doc that was deleted. An alternative is to have a process that sets deleted:True (no underscore) for documents, and to adjust any listener filters, etc., to look for deleted:True. One of the processes can then actually delete the document. This means that any process triggering on the document doesn't need to track an _id for eventual deletion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜