CouchDb由用户按时间戳降序对帖子进行排序 [英] CouchDb sort posts by a user in descending timestamp order

查看:99
本文介绍了CouchDb由用户按时间戳降序对帖子进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的帖子中包含用户名(帖子的作者)和时间戳(上传时间)之类的字段。我正在努力创建一个视图,查询该视图时,该视图将按时间戳的降序抓住特定用户的帖子。

I have posts that have fields like username (author of the post) and timestamp (uploaded time). I am struggling to create a view, which when queried on, grabs posts by a particular user in descending order of the timestamp.

map: function(doc) {
    if (doc.type == 'post'){
        emit(doc.username, [doc._id, doc.timestamp]);
    };
},

我可以查询特定用户创作的文档,但如何应用降序= p仅在时间戳字段上为真?

I can query documents authored by a particular user but how do I apply descending = true only on the timestamp field?

推荐答案

对于CouchDB视图,只有键才能确定索引的排序顺序。该值根本不考虑排序/分组。

With CouchDB views, only the key can determine the sort order for the index. The value does not factor into sorting/grouping at all.

因此,如果要创建一个视图,该视图按创建顺序输出用户的帖子(升序或降序) ),您将发出一个数组作为一种复合键。我强烈建议您通读其中的视图指南 CouchDB文档。

Thus, if you want to have a view that outputs posts by a user in order of creation (ascending or descending), you'll emit an array as a sort of "composite key". I would highly recommend reading through the Guide to Views in the CouchDB documentation.

以您的示例为例,我将使地图功能如下:

For your example, I would make a map function like this:

function (doc) {
  if (doc.type === 'post') {
    emit([ doc.username, doc.timestamp ]);
  }
}

然后,您可以获取特定用户的帖子查询如下:

Then, you can get the posts for a specific user with a query like:

?start_key=["username"]&end_key=["username","\ufff0"]

这将找到按时间戳升序排列的给定用户名的帖子。要颠倒顺序,请改用以下查询:

Which will find the posts for the given "username" ordered by the timestamp ascending. To reverse the ordering, use the following query instead:

?start_key=["username","\ufff0"]&end_key=["username"]&descending=true

请注意, start_key end_key 已互换,并添加了 descending = true

Note that the values for start_key and end_key have swapped, and descending=true has been added.

如前所述,请仔细阅读他们的文档,因为这是绕过使用CouchDB视图的最佳方法的好方法。

As mentioned before, read through their documentation as it's an excellent way to wrap your head around the best way to use CouchDB views.

这篇关于CouchDb由用户按时间戳降序对帖子进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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