8

Following my question here I am exmploring ideas for a generic approach to document versioning in CouchDB. While I imagine there may be no canonical approach, I had the following idea and am looking for feedback.

I would like to maintain readable document ids as much as possible, so a document existing at document1 would contain a pointer document to all existing versions on the system. The actual revision documents would be at something like document1/308ef032a3801a where 308ef032a3801a is some random number or hash.

Example

The pointer document

{
    "_id" : "document1",
    "versions" : [ "document1/308ef032a3801a" ]
}

The version document

{
    "_id" : "document1/308ef032a3801a",
    ... actual content
}
Community
  • 1
  • 1
Jacob Groundwater
  • 6,581
  • 1
  • 28
  • 42

2 Answers2

5

It's more typical to keep older versions of your document inside your current revision (either as JSON or, often, as an attachment). For the JSON case;

{
  "_id":"foo",
  "_rev":"3-fsfsfsdf",
  "foo":"current value of foo",
  "history": {
    "2": {
      "foo":"previous version of foo"
    },
    "1": {
      "foo":"initial version of foo"
    }
  }
}

Obviously this clutters things somewhat, which is why it's often simpler to push the full old version of the document into an attachment instead. This pattern is common enough that CouchDB ships with a library, jquery.couch.js, that implements it (in the saveDoc(doc) function).

Robert Newson
  • 4,631
  • 20
  • 18
  • For a large document, perhaps containing many KB of text, does this method cause unnecessary overhead? Given 100 versions each of which is 1KB in size, what is the total size before compaction? – Jacob Groundwater Nov 27 '11 at 03:46
  • 1
    I'd recommend using the attachment approach, I mention the JSON approach as it's easier to show the example. – Robert Newson Nov 27 '11 at 11:40
2

Here is some discussion about document versioning approaches:

http://jchrisa.net/drl/_design/sofa/_list/post/post-page?startkey=%5B%22Versioning-docs-in-CouchDB%22%5D

The one suggested approach is to stick older versions as attachments to the current doc. As the document mentions, it is simple, scalable, and replicates. The jquery couchdb library has this baked in which is nice.

Ryan Ramage
  • 2,606
  • 18
  • 17
  • fyi web archive link: http://web.archive.org/web/20101203010111/http://jchrisa.net/drl/_design/sofa/_list/post/post-page?startkey=%5B%22Versioning-docs-in-CouchDB%22%5D – Quang Van Jan 21 '17 at 05:43