couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Metson <si...@cloudant.com>
Subject Re: view ordered by DateTime
Date Fri, 04 Jan 2013 10:38:20 GMT
Hey,  
You need to either covert the date into a number (epoch seconds or similar) or make the date
stamp into a date object and emit the pieces, e.g.:

function(doc) {
  date = new Date(Date.parse(doc.date));
  emit([date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDay(), date.getUTCHours(), date.getUTCMinutes()],
doc.text);
}


The advantage of doing the date key as a list is you can have a reduce (like _count) and see
number of documents at the different group levels (year, month, day…)
Cheers
Simon


On Friday, 4 January 2013 at 10:30, ishi soichi wrote:

> couchdb latest
> python 2.7
> ruby 1.9.3
>  
> I have a database obtained from Twitter.
> Each tweet contains the information about when it's created, namely,
> created_at.
>  
> The problem is if I try to view them with the function
>  
> function(doc) {
> if (doc['created_at'] != null) {
> emit(doc['created_at'], doc['text']);
> }
> }
>  
> the tweets are ordered ALPHABETICALLY, not by datetime.
>  
> This is because the data is stored in couchdb in the form of String rather
> than DateTime object.
>  
> How can I order the outputs by DateTime?
>  
> I can program in JavaScript, Ruby(couchrest), and Python, I appreciate
> concrete example using any of these languages.
>  
> soichi  



Mime
View raw message