【问题标题】:How can I sort a request to a view - using nodejs API如何对视图的请求进行排序 - 使用 nodejs API
【发布时间】:2020-01-07 14:09:17
【问题描述】:

我正在从我的 nodejs 应用程序中查询一个 cloudant 数据库。 我现在正在尝试对视图查询的结果进行排序。 我的索引(键)是这样的: [“FR000001”,1577189089166] [“FR000001”,1577189089165] 等等

从以下观点来看:

function(doc) {
  emit([doc.siteId, doc.creationDate],{"id" :doc._id, "rev": doc._rev, "siteId": doc.siteId, "creationDate": doc.creationDate, "scores": doc.scores, locationId: doc.locationId});
}

我使用 cloudant github 的 bugs 部分中的语法,使用语法“sort:”-creationDate“”设法在真实索引上进行了这项工作。

var ddoc = {
    q: "site:\"" + id + "\"",
    include_docs: false,
    sort: "-creationDate",
  };
  const tmp = await cloudant.use('alarms').search('alarmSearch', 'IndexBySite', ddoc);

我无法使用一组查询参数使其在我的视图上工作。我尝试了不同的变化:

var ddoc_view = {
    startkey: ["siteid1",0000000000000],
    endkey: ["siteid1",9999999999999],
    include_docs: true,
    sort: "creationDate" 
  };

谁能帮助找到正确的语法,或者指出我在哪里可以找到好的“cloudant API for nodejs”文档?例如,在 github 上没有关于如何使用排序的内容...谢谢...

【问题讨论】:

    标签: sorting ibm-cloud cloudant


    【解决方案1】:

    经过一天的搜索后确定: - 我找到的最佳文档直接是 couchdb 文档:https://docs.couchdb.org/en/stable/ddocs/views/intro.html - 我最终修改了视图:

    emit([doc.creationDate, doc.siteId], {"id" :doc._id, "rev": doc._rev, "siteId": doc.siteId, "locationTag":doc.locationTag});
    

    和请求:

      var ddoc_view = {
        endkey: [0000000000000, siteid],
        startkey: [9999999999999, siteid],
        include_docs: false,
        descending: true,
        limit: docsReturned,
      };
    

    获得排序的响应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-07
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2021-08-24
      • 1970-01-01
      相关资源
      最近更新 更多