Raghavendra
Raghavendra

Reputation: 128

Link documents using Map-Reduce approch in CouchDB

department {
               "_id": "1", 
               "department": "Computers",
               "type": "Department", 
               "room_no": "102", 
               "HOD": "Mr. G Rahul",
               "floor": "1st Floor" 
            }

student {
             "_id": "fdf370e2f43d4af1b505b8913502a5e4",
             "_rev": "1-16df9a4cd45ca69009ab6c9767425a8e",
             "student Name": "H Ravi",
             "date_of_birth": "March 1, 1993",
             "roll_no": "55",
             "inter_marks": "820",
             "secondary_marks": "420"
             "department_id": "1",
             "type": "student"
         }

Map Function

function(doc) {
              var id,department,student,hod,dob;
              if(doc.type == 'student') {
                      id = doc.department_id;
                      dob = new Date(doc.date_of_birth)
                  student = doc;    
              }
             }
       emit(dob, {'_id': id,"student_doc": student});
     }

After writing map function we call view by using URL "//localhost:5984/db_name/_design/design_name/_view/view_name". In that URL we will append ?include_docs=true after "view_name"("//localhost:5984/db_name/_design/design_name/_view/view_name/?include_docs=true") to get the docs of by using _id in emit, example: emit(dob,{"_id": id}) it will return the docs of linked id...My question is how can we access that docs in reduce function.

Upvotes: 0

Views: 91

Answers (1)

Jan Lehnardt
Jan Lehnardt

Reputation: 2659

You can’t, the docs are fetched on query time, not on indexing time, so the reduce function never gets to see that data. Sorry!

Upvotes: 1

Related Questions