Hi I have two type of documents, say Swimmer and Race.
Here are some swimmer documents:
{
"_class":“Swimmer”,
“swimmerId”:“111”,
“name”:“Michael Phelps”,
“races”:[“123”,“124”,“125”]
}
And some race documents:
{
"_class":“Race”,
“raceId”:“123”,
“name”:“Fina Budapest”,
“distance”:100
},
{
"_class":“Race”,
“raceId”:“124”,
“name”:“Fina Ukraine”,
“distance”:1500
}
I need to create many-to-many relationship but at first, one-to-many relationship is OK. My goal is to query a view for all the races (whole race object) of a swimmer with given swimmerId.
I tried to write some map functions with no success. The missing part is how to simulate join with race id and the race document?
The result should be sth like this when key 111 (swimmerId) is queried:
111 ->
{
"_class":“Race”,
“raceId”:“123”,
“name”:“Fina Budapest”,
“distance”:100
},
{
"_class":“Race”,
“raceId”:“124”,
“name”:“Fina Ukraine”,
“distance”:1500
}
My map function is:
function(doc, meta) {
if (doc._class == ‘Swimmer’) {if (doc.races) { for (var i in doc.races) { emit([meta.id, doc.races[i]], null); } } } else if (doc._class == 'Race') { emit(meta.id, doc); }
}