Devyn
Devyn

Reputation: 2285

CouchDB is not sorting date correctly?

here is my view:

function(doc) {
    if(doc.type=="mail" && doc.user_id == 116 && doc.fid == 81 ){
        emit([doc.time], doc.msg); 
    }
}

I store by default JS Date() and here is the list of doc sorted by descending.

"Wed May 09 2012 00:16:02 GMT+0800 (SGT)"
"Wed May 09 2012 00:10:45 GMT+0800 (SGT)"
"Wed May 02 2012 00:59:47 GMT+0800 (SGT)"

"Tue May 08 2012 23:55:54 GMT+0800 (SGT)"
"Tue May 08 2012 21:59:42 GMT+0800 (SGT)"

"Tue May 01 2012 23:11:57 GMT+0800 (SGT)"
"Tue May 01 2012 19:00:37 GMT+0800 (SGT)"
"Tue May 01 2012 18:59:55 GMT+0800 (SGT)"

**"Sun May 13 2012 16:02:58 GMT+0800 (SGT)"**
"Sun May 13 2012 16:01:16 GMT+0800 (SGT)"

As you can see, date is not sorted properly and "Sun May 13 2012 16:02:58 GMT+0800 (SGT)" is the last entry. How can I fix it? Thanks!

Upvotes: 1

Views: 99

Answers (1)

James C
James C

Reputation: 14149

I think that the date you have in the database will be being stored as a string. If you go into futon and view the source of the page you should be able to confirm this.

In the past I've seen dates stored as unix timestamps to make sorting by integer value easy. If you are indeed storing the data in there as a string this section in the documentation might be of help: http://wiki.apache.org/couchdb/View_collation#Sorting_by_Dates

Upvotes: 2

Related Questions