You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by ishi soichi <so...@gmail.com> on 2013/01/04 11:30:26 UTC

view ordered by DateTime

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

Re: view ordered by DateTime

Posted by Simon Metson <si...@cloudant.com>.
Great :) 


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

> Thanks! It worked.
> 
> soichi
> 
> 2013/1/4 Simon Metson <simon@cloudant.com (mailto:simon@cloudant.com)>
> 
> > date = new Date(Date.parse(doc.date));
> > emit([date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDay(),
> > date.getUTCHours(), date.getUTCMinutes()], doc.text);
> 




Re: view ordered by DateTime

Posted by ishi soichi <so...@gmail.com>.
Thanks! It worked.

soichi

2013/1/4 Simon Metson <si...@cloudant.com>

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

Re: view ordered by DateTime

Posted by Simon Metson <si...@cloudant.com>.
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