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

时间:2015-02-04 10:32:16

标签: javascript json couchdb

下面我提到了设计文件。

       {
        "_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
              }
            ]
      }
   }
]

}

我试过,但错误就像

函数引发错误:(新的TypeError(“row.doc未定义”,“”)) 如何使用couchdb列表函数获取proj_name,proj_start_date和proj_end_date?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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 - 它会自动为每一行发出。