How to get last created document in couchdb?
Asked Answered
B

2

10

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?

Bilander answered 6/3, 2012 at 20:49 Comment(0)
G
17

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.

Gingili answered 10/3, 2012 at 17:20 Comment(0)
S
5

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.

Sumba answered 6/3, 2012 at 23:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.