且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

CouchDB:根据时间戳返回最新类型的文档

更新时间:2023-11-26 18:21:28

您可以使用 _stats内置reduce函数,然后再做一次查询来获取文档.以下是观点:

You can get the latest timestamp for every source using the _stats built-in reduce function, then do another query to get the documents. Here's the views:

"views": {
  "latest_update": {
    "map": "function(doc) { if (doc.type == 'status_update') emit(doc.source_id, doc.timestamp); }",
    "reduce": "_stats"
  },
  "status_update": {
    "map": "function(doc) { if (doc.type == 'status_update') emit([doc.source_id, doc.timestamp], 1); }"
  }
}

首先使用 group=true 查询 latest_update,然后使用类似(正确的 url 编码)查询 status_update:

First query latest_update with group=true, then status_update with something like (properly url-encoded):

keys=[["truck123",TS123],["truck234",TS234],...]&include_docs=true

其中 TS123 和 TS234 是 latest_update 返回的 max 的值.

where TS123, and TS234 are the values of max returned by latest_update.