【问题标题】:Cloudant Query or Map/Reduce when getting latest documents获取最新文档时的 Cloudant Query 或 Map/Reduce
【发布时间】:2015-07-16 20:53:36
【问题描述】:

我的 Cloudant 数据库有多种类型,每种类型都有多个文档。文档有一个时间戳字段。假设类型是“A”、“B”和“C”。我只想获取每种类型的最新文档,而不是所有文档。换句话说,我只想要三个文件。

这个结果将在浏览器中与 Pouchdb 同步。

我使用 Cloudant Query 还是 Map/Reduce?

【问题讨论】:

    标签: pouchdb cloudant


    【解决方案1】:

    解决问题的方法不止一种。在您的情况下,您可以使用 Cloudant Query 或 map/reduce,但 Cloudant Query 可能更简单。

    您的索引将如下所示:

    {
      "index": {
        "fields": ["type", "timestamp"] 
      }
    }
    

    那么您的查询将是:

    {
      "selector": {"type": "A"},
      "sort": [{"timestamp": "desc"}],
      "limit": 3
    }
    

    如果您想以一种简单的方式使用 Cloudant 查询语言,请查看the pouchdb-find demo page

    【讨论】:

    • 误解了问题。该解决方案为我提供了最新的三个相同类型。寻找三种不同类型中的每一种的最新版本。看起来我必须运行三个不同的查询。在现实世界中运行不同的查询变得很困难,我不知道有多少不同的类型。我得看看 reduce 函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    相关资源
    最近更新 更多