在CouchDB中链接文档

时间:2014-06-25 10:54:22

标签: javascript couchdb couchdb-futon

我的couch-db中有两种类型的文件(都在同一个数据库中),现在我创建了一个视图,它可以通过“_id”链接两个文档,并使用couch-db视图URL返回数据。现在我想从map函数中获取两种类型的数据。文档和代码的类型如下所示。

文档

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"
         }

地图-功能

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;    
         }
         if(doc.type == 'department') {
           if(doc._id == 'id') {
           hod = doc.HOD;
               department = doc;
         }
         }
   emit([dob,hod], {'_id': id,"student_doc": student,"depart_doc":department});
 }

第二个 if 条件未在上述函数中执行。以上代码仅为 map-function

1 个答案:

答案 0 :(得分:0)

将两个测试包装在一个if语句中:

function(doc) {
    var id,department,student,hod,dob;
    if(doc.type) {
      if(doc.type === 'student') { ... }
      if(doc.type === 'department') { ... }
    }
    emit([dob,hod], {'_id': id,"student_doc": student,"depart_doc":department});
 }