1

我在 couchdb 存储中有以下 json(简化)对象:

[{
    "_id": "5ea7a53e670b432e0fe22a7bc10024db",
    "_rev": "1-ae70c8906f7aa6d73539a89f7ad960ee",
    "type": "job"
}, {
    "_id": "5ea7a53e670b432e0fe22a7bc10041d9",
    "_rev": "4-fa0ba68c35ca548b497a7309389f9087",
    "type": "scan",
    "job_id": "5ea7a53e670b432e0fe22a7bc10024db",
    "number": 1
}, {
    "_id": "5ea7a53e670b432e0fe22a7bc100520e",
    "_rev": "4-3e6b1a028786c265ecb7362e245d049e",
    "type": "scan",
    "job_id": "5ea7a53e670b432e0fe22a7bc10024db",
    "number": 2
}]

我想使用键 ["5ea7a53e670b432e0fe22a7bc10024db", 2] (作业 ID 和扫描号)发出一个发布请求。如何为视图制作地图功能以过滤出具有给定 id 的作业以及与 job_id 和数字匹配的测量值?

谢谢,拉杜

4

1 回答 1

1

您期望的请求输出是什么?如果您只想进行扫描,请在map要搜索的键中发出:

function (doc) {
  if (type == "scan" && number) {
    emit([doc.job_id, doc. number], doc);
  }
}

如果您需要两个文档 - 作业(完整文档,而不仅仅是)和扫描,请在请求的 URL中使用参数id将它们单独发出:emitinclude_docs=true

function (doc) {
  if (doc.type == "scan" && doc.number) {
    emit([doc.job_id, doc. number], {scan: doc, _id: doc.job_id});
  }
}

或在两个emit中:

function (doc) {
  if (doc.type == "scan" && doc.number && doc.job_id) {
    emit([doc.job_id, doc. number, "job"], {_id: doc.job_id);
    emit([doc.job_id, doc. number, "scan"], {_id: doc._id});
  }
}

您将在 URL 中获得带有startkey=["5ea7a53e670b432e0fe22a7bc10024db", 2]&endkey=["5ea7a53e670b432e0fe22a7bc10024db", 2, {}]&include_docs=true(或)的两个文档。keys=[]

检查选项的视图 APIinclude_docs

于 2012-09-09T16:46:19.977 回答