You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by felix milea <fe...@gmail.com> on 2012/03/10 15:24:03 UTC

get by value, order by time

Hey guys, I'm new to CouchDB and I need help with an app I'm working on;
I'm trying to get the 50 most recent posts a user made in the app. I
created a view that pulls out the documents for the posts, and I can use
the key parameter to get only the posts of a particular user and the limit
parameter to get only 50 entries.

However, I'd like to order the returned docs by a "timestamp" field (which
stores the new Date().getTime() of when the entry was made) in order to
ensure that the posts are the most recent ones. How can I do this in
CouchDB?

-- 
Felix Milea-Ciobanu

RE: get by value, order by time

Posted by John Hammond <jo...@hotmail.com>.
You can use the following if you have a created_at attribute and sort by
datetime.


emit(Date.parse(doc.created_at).getTime(), doc);

-----Original Message-----
From: felix milea [mailto:felixmilea@gmail.com] 
Sent: Saturday, March 10, 2012 9:24 AM
To: user@couchdb.apache.org
Subject: get by value, order by time

Hey guys, I'm new to CouchDB and I need help with an app I'm working on; I'm
trying to get the 50 most recent posts a user made in the app. I created a
view that pulls out the documents for the posts, and I can use the key
parameter to get only the posts of a particular user and the limit parameter
to get only 50 entries.

However, I'd like to order the returned docs by a "timestamp" field (which
stores the new Date().getTime() of when the entry was made) in order to
ensure that the posts are the most recent ones. How can I do this in
CouchDB?

--
Felix Milea-Ciobanu


Re: get by value, order by time

Posted by felix milea <fe...@gmail.com>.
Thanks for the quick reply! :)
I was able to figure it out by modifying my map function to return
[doc.username, doc.timestamp] as the key, then in my query I used
startkey=[username, new Date().getTime()] (I'm using nodejs),
descending=true and limit=50, and now it seems to work.

On Sat, Mar 10, 2012 at 10:46 AM, Matthieu Rakotojaona <
matthieu.rakotojaona@gmail.com> wrote:

> Hi Felix,
>
> On Sat, Mar 10, 2012 at 3:24 PM, felix milea <fe...@gmail.com> wrote:
> > Hey guys, I'm new to CouchDB and I need help with an app I'm working on;
> > I'm trying to get the 50 most recent posts a user made in the app. I
> > created a view that pulls out the documents for the posts, and I can use
> > the key parameter to get only the posts of a particular user and the
> limit
> > parameter to get only 50 entries.
> >
> > However, I'd like to order the returned docs by a "timestamp" field
> (which
> > stores the new Date().getTime() of when the entry was made) in order to
> > ensure that the posts are the most recent ones. How can I do this in
> > CouchDB?
> >
> > --
> > Felix Milea-Ciobanu
>
> If you already have a view that gives you what you want, you can add
> your timestamp to the keys emitted. You will then have to query your
> view with these parameters :
>
> * group_level = 1 (group by user, each document on its own row)
> * descending = true (to get the most recent first)
> * limit = 50 (to fetch only those you need)
>
> and the key would be {"key":["<username>", {} ] }
>
> Note : I don't know if 'descending = true' is smart enough to only
> fetch relevant docs, unlike 'skip = XX'
>
> --
> Matthieu RAKOTOJAONA
>



-- 
Felix Milea-Ciobanu

Re: get by value, order by time

Posted by Matthieu Rakotojaona <ma...@gmail.com>.
Hi Felix,

On Sat, Mar 10, 2012 at 3:24 PM, felix milea <fe...@gmail.com> wrote:
> Hey guys, I'm new to CouchDB and I need help with an app I'm working on;
> I'm trying to get the 50 most recent posts a user made in the app. I
> created a view that pulls out the documents for the posts, and I can use
> the key parameter to get only the posts of a particular user and the limit
> parameter to get only 50 entries.
>
> However, I'd like to order the returned docs by a "timestamp" field (which
> stores the new Date().getTime() of when the entry was made) in order to
> ensure that the posts are the most recent ones. How can I do this in
> CouchDB?
>
> --
> Felix Milea-Ciobanu

If you already have a view that gives you what you want, you can add
your timestamp to the keys emitted. You will then have to query your
view with these parameters :

* group_level = 1 (group by user, each document on its own row)
* descending = true (to get the most recent first)
* limit = 50 (to fetch only those you need)

and the key would be {"key":["<username>", {} ] }

Note : I don't know if 'descending = true' is smart enough to only
fetch relevant docs, unlike 'skip = XX'

-- 
Matthieu RAKOTOJAONA