In my couch I have document pairs like this:
{
_id: "DOCID",
type: "Task",
info: { k1: "v1", k2: "v2" }
}
{
_id: "ANOTHER DOCID",
type: "Final",
task: "DOCID",
author: "Authorname"
}
For an author, several of these pairs can exist.
I now need a view which will give me the information coupled in a way, that the author
is accompanied by the info
.
Using view collocation I created the following view:
function(doc) {
if (doc.doc_type == "Final")
emit([doc.task, 0], doc.author);
if (doc.doc_type == "Task")
emit([doc._id, 1], doc.definition);
}
And I get results like these:
["153b46415108e95c811e1d4cd018624f", 0] -> "Authorname"
["153b46415108e95c811e1d4cd018624f", 1] -> { info here }
First, I used a reduce function to incorporate both into one, but after timing it, grouping them locally is way faster.
However, the way it is now, I cannot query this view by the "Authorname". Especially not because the info
does not come with an Authorname.
So there are some solutions to this I think:
- Use a reduce function with grouping and manipulate the key so it shows the author (I dont even know if manipulating a grouped key is possible)
- Get all the rows, group them locally, and filter for the Author I am looking for (probably too much unwanted overhead)
- Have multiple views and perform 2 queries. One to get the DOCIDs and then query for the DOCIDs.
- Query the collocated view smartly: include the Authorname into the key and kind of query that in an efficient way, but I also don't think this is possible, as a query for Authorname will exclude the the actual
info
.
So what would you recommend to go on about this?
And yes there is a reason for why the information is separate (several Final
docs can be related to the same Task
document, hence having the same information)
Best
Edit The provided solution, does answer my question, but I went for using my view and grouping the results in my code (a Django View) which turned out to be really fast!