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

How to find matching datas from two json documents in couchdb?

本文关键字:文档 json 两个 数据 couchdb      更新时间:2023-09-26

如何从两个json文档中找到匹配的数据。例如:我有两个json文档和skill-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"]
}

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

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

对于skillCodesemployee实例的构造函数,以上内容可能会变得更加面向对象,我将留给您。