I’m having problems trying to get my head around getting a collection of types along with number of times a skill is found in that doc type.
There are a number of document types that have a list of skills.
{
"skills": "Windows, Network Admin, Linux",
"type": "Experience"
},
{
"skills": "Windows, Erlang, Linux",
"type": "Experience"
},
{
"skills": "Ruby, Rails, Erlang",
"type": "Project"
}
I’m trying to get the number of times a skill is found in an document type.
The end result should look something like this:
{
'type': Experience,
'skills': [
{'skill': 'Erlang', 'count': 1},
{'skill': 'Linux', 'count': 2},
{'skill': 'Network Admin', 'count': 1},
{'skill': 'Rails', 'count': 0},
{'skill': 'Ruby', 'count': 0},
{'skill': 'Windows', 'count': 2}
]
},
{
'type': Project,
'skills': [
{'skill': 'Erlang', 'count': 1},
{'skill': 'Linux', 'count': 0},
{'skill': 'Network Admin', 'count': 0},
{'skill': 'Rails', 'count': 1},
{'skill': 'Ruby', 'count': 1},
{'skill': 'Windows', 'count': 0}
]
}
What would be the best way to do this?
Here are map and reduce functions (view named
skill_split) and a list function that will produce output close to what you asked for using a URL like thishttp://127.0.0.1:5984/myskills/_design/myskills/_list/transform/skill_split?group=true:I can tweak the code to make the output exactly what you asked for, but the list function would be a bit longer.
map:
reduce:
or use
"reduce": "_sum"for shortlist (named
transform):If you don’t have access to the JSON object (you do inside a CouchApp), you might have to jump through some hoops to get access to it.