简体   繁体   中英

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

How to find matching datas from two json documents.For ex: I have two json documents and skills json documents.

In Skills Document:

{

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

In Employee Document:

{

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

I have no idea what you want to do, but the following might be helpful. Assuming that the first data set is a list of skills and their descriptions and the second is an employee record, then assigning to variables with suitable names might look like:

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"]
};

Creating a skills index makes looking for particular skills much simpler, some code to do that is:

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

Now all that is required is a function to get the skills for a particular employee, say:

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]);
  });
}

An example:

getCoreSkills(employee0);

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

The above could become much more OO given constructors for skillCodes and employee instances, I'll leave that to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM