简体   繁体   English

在CouchDB视图中排序日期

[英]Sorting Dates in CouchDB Views

I have a nested JSON object for the key status below: 我有一个嵌套的JSON对象,用于下面的密钥状态

{
"2011-01-19 09:41:00 AM": "Prototyping status application",
"2011-01-20 09:41:00 AM": "Playing with CouchDB"
}

It's a little application where user can enter his/her status. 这是一个小应用程序,用户可以输入他/她的状态。 I want to get the most recent status from it. 我希望从中获得最新状态。 Is this approach good for such applications, or do I have to have a key that defines sort order? 这种方法对于这样的应用程序是否有用,或者我必须有一个定义排序顺序的键?

What's the best way to get the most recent date? 获取最近日期的最佳方式是什么?

Thanks 谢谢

Use view collation and emit a complex key. 使用视图排序并发出复杂的键。

If you first want to sort by user, then by time, use this as key. 如果您首先要按用户排序,则按时间使用此键作为键。 If you only want to sort by time, omit the username as key. 如果您只想按时间排序,请省略用户名作为密钥。

If you're using Date-style values: 如果您使用日期样式值:

emit([Date.parse(doc.created_at).getTime(), doc.username], doc);

If you use a date format that is already sortable lexicographically: 如果您使用已按字典顺序排序的日期格式:

emit([doc.created_at, doc.username], doc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM