Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last created document in couchdb?

Tags:

couchdb

How can I get last created document in couchdb? Maybe some how I can use _changes feature of couchdb? But documentation says, that I only can get list of document, ordered by first created document, ant there is no way to change order.

So how can I get last created document?

like image 357
sirex Avatar asked Mar 06 '12 20:03

sirex


2 Answers

Your only surefire way to get the last created document is to include a timestamp (created_at or something) with your document. From there, you just need a simple view to output all the docs by their creation date.

I was going to suggest using the last_seq information from the database, but the sequence number changes with every single write, and replication also complicates the matter further.

like image 178
Dominic Barnes Avatar answered Sep 20 '22 18:09

Dominic Barnes


You can get the changes feed in descending order as it's also a view.

GET /dbname/_changes?descending=true

You can use limit= as well, so;

GET /dbname/_changes?descending=true&limit=1

will give the latest update.

like image 45
Robert Newson Avatar answered Sep 18 '22 18:09

Robert Newson