如何在couchdb中找到两个json文档的匹配数据?

时间:2014-10-17 06:25:07

标签: javascript couchdb couchdb-futon

如何从两个json文档中查找匹配数据。例如:我有两个json文档和技巧json文档。

技能文件:

{

     "_id": "b013dcf12d1f7d333467b1447a00013a",
     "_rev": "3-e54ad6a14046f809e6da872294939f12",
     "core_skills": [
          {
              "core_skill_code": "SA1",
              "core_skill_desc": "communicate with others in writing"
          },
          {
              "core_skill_code": "SA2",
              "core_skill_desc": "complete accurate well written work with attention to detail"
          },
          {
              "core_skill_code": "SA3",
              "core_skill_desc": "follow guidelines/procedures/rules and service level agreements"
          },
          {
              "core_skill_code": "SA4",
              "core_skill_desc": "ask for clarification and advice from others"
          }
      ]}

在员工文档中:

{

  "_id": "b013dcf12d1f7d333467b12350007op",
  "_rev": "3-e54ad6a14046f809e6da156794939f12",
  "employee_name" :"Ashwin",
  "employee_role" : "Software engineer",
  "core_skills":["SA1","SA4"]
}

1 个答案:

答案 0 :(得分:0)

我不知道你想做什么,但以下内容可能会有所帮助。假设第一个数据集是技能及其描述的列表,第二个是员工记录,那么分配给具有合适名称的变量可能如下所示:

var skillCodes = {
  "_id": "b013dcf12d1f7d333467b1447a00013a",
  "_rev": "3-e54ad6a14046f809e6da872294939f12",
  "core_skills": [{
      "core_skill_code": "SA1",
      "core_skill_desc": "communicate with others in writing"
    },{
      "core_skill_code": "SA2",
      "core_skill_desc": "complete accurate well written work with attention to detail"
    },{
      "core_skill_code": "SA3",
      "core_skill_desc": "follow guidelines/procedures/rules and service level agreements"
    },{
      "core_skill_code": "SA4",
      "core_skill_desc": "ask for clarification and advice from others"
    }
  ]};

var employee0 = {
  "_id": "b013dcf12d1f7d333467b12350007op",
  "_rev": "3-e54ad6a14046f809e6da156794939f12",
  "employee_name" :"Ashwin",
  "employee_role" : "Software engineer",
  "core_skills":["SA1","SA4"]
};

创建技能指数使得寻找特定技能变得更加简单,一些代码就是:

var skillCodeIndex = {};
skillCodes.core_skills.forEach(function(item){
  skillCodeIndex[item.core_skill_code] = item.core_skill_desc;
});

现在所需要的只是获得特定员工技能的功能,例如:

function getCoreSkills (employee) {
  console.log('Employee ' + employee.employee_name + ' has the following core skills:');
  employee.core_skills.forEach(function(skill) {
    console.log(skill + ': ' + skillCodeIndex[skill]);
  });
}

一个例子:

getCoreSkills(employee0);

Employee Ashwin has the following core skills:
SA1: communicate with others in writing
SA4: ask for clarification and advice from others

对于 skillCodes 员工实例的构造函数,上面的内容可能会更多,我会留给您。