【问题标题】:Need a CouchDB trick to sort by date and filter by group需要一个 CouchDB 技巧来按日期排序并按组过滤
【发布时间】:2012-04-13 14:36:38
【问题描述】:

我有包含“日期”和“组”字段的文档。这是我的观点:

byDateGroup: {
  map: function(doc) {
    if (doc.date && doc.group) {
      emit([doc.date, doc.group], null);
    }
  }
}

这个等价的查询是什么:

select * from docs where group in ("group1", "group2") order by date desc

我没有想到这个简单的解决方案。 :(

【问题讨论】:

    标签: view map couchdb mapreduce


    【解决方案1】:

    Pankaj,将您发出的键的顺序切换为:

    emit([doc.group, doc.date], doc);
    

    然后你可以在查询视图时传入一个开始键和一个结束键。为要为其提取数据的每个组执行单独的查询可能会更容易。数据将按日期排序。

    我目前正在外出,但如果不清楚,可以在回来时详细说明。

    【讨论】:

    • +1 这似乎是最好的解决方案。从根本上说,Jangid 要求加入,因此多个请求和“加入”回客户端可能是最直接的事情。
    • 同意。这是目前最好的可用方法。除了@JasonSmith 所说的之外,您还可以在 _list 函数中进行额外的过滤。在所有情况下,CouchDB 都会“自动排序”所有日期键(时间戳或日期数组)。 “困难的部分”只是从索引中挑选出特定的组。
    【解决方案2】:

    为什么不呢?

    function(doc) {
      if (doc.date && ["group1", "group2"].indexOf(doc.group) !== -1)) {
        emit([doc.date, doc.group], null);
      }
    }
    

    【讨论】:

      【解决方案3】:

      为此您需要一个特定的视图:

      byDateGroup1Group2: {
        map: function(doc) {
          if (doc.date && doc.group && (doc.group === "group1" || doc.group === "group2") {
            emit(doc.date, doc);
          }
        }
      }
      

      您使用查询 descending=true 查询(可能在列表函数中)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-24
        • 2011-03-08
        • 1970-01-01
        • 2021-06-24
        • 2019-11-28
        • 1970-01-01
        • 2012-12-13
        相关资源
        最近更新 更多