incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan-Felix Wittmann <jfwittm...@gmx.de>
Subject Re: sort by key
Date Tue, 22 Sep 2009 15:32:50 GMT
Hi Dmitriy,

Am 22.09.2009 um 14:59 schrieb Dmitriy Novotochinov:

> Please help me to implement sorting by key.
>
> Here is view function:
>
> function(doc){
>     if (doc.user && doc.user.name) {
>       emit(doc.user.name, doc);
>   }
> }
>
> Query all docs where user.name = user_name:
>
> /_design/users/_view/by_name?key=user_name
>
> Query returns docs sorted by key doc.user.name, but how to do  
> sorting by doc.created_at, for example?
>

first you could create a second view like:

/_design/users/_view/by_date

function(doc){
       emit(created_at, doc);
}

or if you want  results that are sorted by date and username you could
emit an array as key.

e.g.

function(doc){
     if (doc.user && doc.user.name) {
       emit([doc.created_at, doc.user.name], doc);
   }
}

your key (or startkey, endkey) is now a json encoded array.

--Felix


Mime
View raw message