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

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

问题描述

我在下面的键状态中有一个嵌套的JSON对象:

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

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

{
"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?

谢谢

推荐答案

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

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天全站免登陆