【问题标题】:How to find matching datas from two json documents in couchdb?如何从 couchdb 中的两个 json 文档中找到匹配的数据?
【发布时间】:2014-10-17 06:25:06
【问题描述】:

如何从两个 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"]
}

【问题讨论】:

  • 那么您要匹配的数据是什么?
  • 我需要匹配核心技能数据。
  • 你需要详细说明你想要做什么。
  • 创建核心技能代码和描述的索引。然后你就可以破译员工记录中的代码了。

标签: javascript couchdb couchdb-futon


【解决方案1】:

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

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 实例的构造函数,上述内容可能会变得更加面向对象,我将把它留给你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 2021-04-12
    相关资源
    最近更新 更多