couchdb obtener por valor, límite y orden por tiempo

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?

preguntado el 10 de marzo de 12 a las 08:03

1 Respuestas

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 colación will make sure the results are first ordered by user and then by date.

respondido 10 mar '12, 14:03

thank you! that worked beautifully :) I did not know you can use an array as the key, very useful to know! once again, thank you!! - Felix Mc

oops, noticed a small bug..you need to also include a endkey of ['johndoe', 0] in order to prevent couchdb from returning posts from usernames which start with 'johndoe' like 'johndoe59'. - Felix Mc

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.