Felix Mc
Felix Mc

Reputation: 503

couchdb get by value, limit, and order by time

I'm new to CouchDB and I'm trying to get the last 50 most recent entries of a user in an app. I created a view that pulls out the documents for the entries, and I can use the key parameter to get only the docs of a particular user and the limit query to get only 50 entries.

However, I'd like to order the docs by a "timestamp" field (which stores the new Date().getTime() of when the entry was made) in order to ensure that I only get the most recent entries. Is this possible in CouchDB, and if so how?

Upvotes: 0

Views: 646

Answers (1)

Antonis Anastasiadis
Antonis Anastasiadis

Reputation: 269

You can probably achieve this (at least in the case that you don't have any future dates in your data) by emitting a more complex key like an array of the form [username,datetime]. Then make a view that pulls the documents with a startkey like, for example, ['johndoe',1331388874195] with descending order, and limit to 50. The date should obviously be the current one. CouchDB's collation will make sure the results are first ordered by user and then by date.

Upvotes: 1

Related Questions