如何使用 couchdb 列表函数从 couchdb 视图结果中获取特定字段

How to get the particular field from the couchdb views result using couchdb list function

本文关键字:couchdb 获取 字段 结果 视图 何使用 列表 函数      更新时间:2023-09-26

下面我提到了设计文档。

       {
        "_id": "_design/link",
        "_rev": "62-0c0f00dd9dbedab5c2cca61c356bbff4",
        "views": {
         "link": {
           "map": "function(doc) {'n if (doc.projects) { for (var i in    doc.projects) { emit(doc._id, {_id: doc.projects[i].proj_id}); }} 'n}"
},
          "lists": {
          "sample": "function(head, req) {while(row = getRow()){  send(row.doc.proj_name);} }"
}
}

}

查看结果:

{
total_rows: 1,
offset: 0,
rows: [
{
 id: "SCI130202",
 key: "SCI130202",
 value: {
       _id: "PID00034"
        },
 doc: {
     _id: "PID00034",
     _rev: "1-0a363e98a605a72fd71bb4ac62e0b138",
     client_id: "E000022",
     client_name: "Edinburgh Steel",
     type: "manage projects",
     proj_id: "PID00034",
     proj_name: "Global_upgrade_Oracle",
     proj_domain: "Information Technology",
     proj_start_date: "2014-10-08",
     proj_end_date: "2015-07-07",
     delivery_manager: null,
     proj_standards: null,
     proj_currency_type: "INR",
     onsite: "No",
     location: "Edinburgh",
     proj_status: "Noy yet Start",
     budgeted_margin: 45,
     budgeted_hrs: 300,
     projected_revenue: 200000,
     billing_rate: 30,
     unit_measure: "per month",
     billing_cycle: "Milestone",
     proj_core_tech_skills: [ ],
     proj_secon_skills: [ ],
     proj_sdlc_skills: [ ],
     tag: "",
     margin: [
              {
                desired_onsite: null,
                desired_offshore: null,
                lower_limit: null
              }
            ]
      }
   }
]

}

我试过了,但错误来了

函数引发错误:(新类型错误("行.doc未定义", "))如何使用 couchdb 列表功能获取proj_name、proj_start_date 和proj_end_date?

您需要将include_docs=true选项添加到用于查询视图/列表的 URL 中。视图不会自动包含文档。

也许你不应该使用列表来过滤你的视图结果 - 只要让视图发出你需要的东西:

emit(doc._id, {
  _id: doc.projects[i].proj_id
});

变成:

emit(doc.proj_id, {
  proj_name: doc.proj_name,
  proj_id: doc.proj_id,
  proj_start_date: doc.proj_start_date,
  proj_end_date: doc.proj_end_date
});

您无需发出doc._id - 它会自动为每一行发出。