How to get only the latest revision of a document

1.2k Views Asked by At

I am new to CouchDb/ektorp. According to the ektorp documentation you can generate couchdb views through an annotation like this:

@View( name = "avg_sofa_size", map = "function(doc) {...}", reduce = "function(doc) {...}")

Is there a way to get only the latest revison of each doc, not by id but another attribute? <-- not java specific

And if possible only get only one doc not by id but another attributefrom that view? <-- ektorp/java

Thakns

2

There are 2 best solutions below

2
On

Is there a way to get only the latest revison of each doc?

Couch always gives you the latest revision, unless you ask for a particular revision:

GET http://localhost:5984/mydb/doc-123

Returns latest revision of doc-123

GET http://localhost:5984/mydb/doc-123?rev=946B7D1C

Returns a specific revision of doc-123. (Revisions are kept by couch only for conflict resolution. They are removed during compaction and are not replicated, so you shouldn't necessarily count on them being there)

And if possible only get only one doc by id from that view

If you just want to get a document by id, there is no need to use a view. Looks like you want to do something like this with ektorp:

Sofa sofa = db.get(Sofa.class, id, rev);

Where Sofa is a java class which extends CouchDbDocument

0
On

As stated in the other answer, you will always get the doc with the latest revision if you don't explicit specify the revision.

Sofa sofa = db.get(Sofa.class, id);