在 CouchDB 视图中对日期进行排序 [英] Sorting Dates in CouchDB Views

查看:22
本文介绍了在 CouchDB 视图中对日期进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的 JSON 对象,用于下面的键 status:

I have a nested JSON object for the key status below:

<代码>{
"2011-01-19 09:41:00 AM": "原型状态申请",
2011-01-20 09:41:00 AM":玩 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?

谢谢

推荐答案

使用 查看排序规则 并发出一个复杂的密钥.

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);

这篇关于在 CouchDB 视图中对日期进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆