3

I am very new to CouchDB. Missing SQL already.

Anyways, I need to create a view that emits a few attributes of my documents along with all the revision IDs.

Something like this

function(doc) {
    if (doc.type == 'template') {
        emit(doc.owner, {_id: doc._id, _rev: doc._rev, owner: doc.owner, meta: doc.meta, key: doc.key, revisions_ids: What goes here?});
    }
}

But how do I tell it to include all the revisions?

I know I can call http://localhost:5984/main/94c4db9eb51f757ceab86e4a9b00cddf

for each document (from my app), but that really does not scale well.

Is there a batch way to fetch revision info?

Any help would be appreciated!!

Tom
  • 16,842
  • 17
  • 45
  • 54
GoneFishing
  • 31
  • 1
  • 2

1 Answers1

7

CouchDB revisions are not intended to be a version control system. They are only used for ensuring write consistency. (and preventing the need for locks during concurrent writes)

That being said, only the most recent _rev number is useful for any given doc. Not only that, but a database compaction will delete all the old revisions as well. (a compaction is never run automatically, but is should be part of routine maintenance)

As you may have already noticed, your view outputs the most recent _rev number in the value of your view output. Also, if you are using include_docs=true, then the _rev number is also shown in the doc portion of your view result.

Strategies do exist for using CouchDB for revision history, but they are generally complicated, and not usually recommended. (check out this question and this blogpost for more information on that subject)

Community
  • 1
  • 1
Dominic Barnes
  • 28,083
  • 8
  • 65
  • 90
  • Thank you for educating me on this fact. I'm new to Couch and inherited a system at work from a colleague that left the company. I think the approach he chose when designing the system is to assume there will only be 1 node and never compact. – GoneFishing Feb 06 '15 at 18:14
  • I'm savvy with oracle and think I can rebuild the back end with it, but are there more appropriate noSQL systems that would be a better fit for tracking versions? – GoneFishing Feb 06 '15 at 18:15
  • 3
    more on CouchDB not being a version control system, you got to watch this great conference for Couch newcomers on [10 Common Misconceptions about CouchDB](http://youtu.be/BKQ9kXKoHS8?t=11m05s): point 4 (11:05) is exactly on this question. Joan Touzet's suggestion: *"If you need it, keep it. Create a new document for it. Don't overwrite/delete it. Write a new document for every single transaction. Views can then help you find the latest info."* The whole presentation is definitely worth your time if you choose to keep working with CouchDB – maxlath Feb 06 '15 at 20:22