Hi, having some trouble with map/reduce returning an array of values,
and was wondering if you might be able to help:
Documents in my DB have an optional "parent" value, which indicates the
parent document's id.
For example, some simplified documents and ids:
{ "_id": "a" },
{
"_id": "b",
"parent": "a"
},
{
"_id": "c",
"parent": "a"
},
{ "_id": "d" },
{
"_id": "e",
"parent": "d"
}
Given a doc id, I am trying to retrieve a list of its "child" document
ids, which point to the specified doc as their parent.
For example, with the docs above, querying for "a" should return ["b",
"c"].
Querying for "d" should return ["e"]
The map function I tried was:
function(doc) {
if (doc.parent) {
emit (doc.parent, doc._id);
}
}
Which works fine; here is example output for:
/db/_design/temp/_view/related?key="a"
{
total_rows: 12,
offset: 3,
rows: [
{
id: "b",
key: "a",
value: "b"
},
{
id: "c",
key: "a",
value: "c"
}
]
}
However, I'm unsure what to use for a reduce function.
This was my last attempt, which seemed to work correctly for a while:
function(keys, values) {
var related = [];
for (var i = 0; i < values.length; ++i) {
if (related.indexOf (values[i]) == -1) {
related.push (values[i]);
}
}
return related;
}
However, after adding a few dozen documents, it stopped functioning with
this error:
"Reduce output must shrink more rapidly: "
If there some way to correct my reduce or map function? Or is there
another approach I could use? (Barring changing the document structure
if possible)
Thanks