I think I understand what you are asking. The answer is pretty straightforward with Map/Reduce.
Say you have the following people documents:
{
"name": "Person A",
"interests" [ "computers", "fishing", "sports" ]
}
{
"name": "Person B",
"interests" [ "computers", "gaming" ]
}
{
"name": "Person C",
"interests" [ "hiking", "sports" ]
}
{
"name": "Person D",
"interests" [ "gaming" ]
}
You would probably want to emit your key as the interest, with the value as the person's name (or _id
).
function (doc) {
for (var x = 0, len = doc.interests.length; x < len; x++) {
emit(doc.interests[x], doc..name);
}
}
Your view results would look like this:
- computers => Person A
- computers => Person B
- fishing => Person A
- gaming => Person B
- gaming => Person D
- hiking => Person C
- sports => Person A
- sports => Person C
To get a list of people with computers as an interest, you can simply send key="computers"
as part of the query string.
If you want to add a reduce function to your map, you can simply use _count
(shortcut to use a compiled reduce function) and you can retrieve a count of all the people with a particular interest, you can even use that to limit which interests you query to build your relationships.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…