Are the old data accessible in CouchDB?

 1 year ago
source link: https://www.codesd.com/item/are-the-old-data-accessible-in-couchdb.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Are the old data accessible in CouchDB?


I've read a bit about CouchDB and I'm really intrigued by the fact that it's "append-only". I may be misunderstanding that, but as I understand it, it works a bit like this:

  • data is added at time t0 to the DB telling that a user with ID 1's name is "Cedrik Martin"

  • a query asking "what is the name of the user with ID 1?" returns "Cedrik Martin"

  • at time t1 an update is made to the DB telling: "User with ID 1's name is Cedric Martin" (changing the 'k' to a 'c').

  • a query asking again "what is the name of the user with ID 1" now returns "Cedric Martin"

It's a silly example, but it's because I'd like to understand something fundamental about CouchDB.

Seen that the update has been made using an append at the end of the DB, is it possible to query the DB "as it was at time t0", without doing anything special?

Can I ask CouchDB "What was the name of the user with ID 1 at time t0?" ?

EDIT the first answer is very interesting and so I've got a more precise question: as long as I'm not "compacting" a CouchDB, I can write queries that are somehow "referentially transparent" (i.e. they'll always produce the same result)? For example if I query for "document d at revision r", am I guaranteed to always get the same answer back as long as I'm not compacting the DB?

Perhaps the most common mistake made with CouchDB is to believe it provides a versioning system for your data. It does not.

Compaction removes all non-latest revisions of all documents and replication only replicates the latest revisions of any document. If you need historical versions, you must preserve them in your latest revision using any scheme that seems good to you.

"_rev" is, as noted, an unfortunate name, but no other word has been suggested that is any clearer. "_mvcc" and "_mcvv_token" have been suggested before. The issue with both is that any description of what's going on there will inevitably include the "old versions remain on disk until compaction" which will still imply that it's a user versioning system.

To answer the question "Can I ask CouchDB "What was the name of the user with ID 1 at time t0?" ?", the short answer is "NO". The long answer is "YES, but then later it won't work", which is just another way of saying "NO". :)

About Joyk

Aggregate valuable and interesting links.
Joyk means Joy of geeK