How to get last created document in couchdb?

2020-08-23 00:55发布

问题:

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?

回答1:

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.



回答2:

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.



标签: couchdb