在PouchDB中加入(链接)文档

时间:2018-03-30 09:34:53

标签: pouchdb

我使用CouchDB作为后端,使用PouchDB在vuejs应用程序中查询/同步数据库。

我有4种不同类型的文档都存储在一个DB中,如上所述:

{
  "_id": "patientmed_001",
  "_rev": "1-b892ba78a095627af8aef65ac279cc84",
  "doctype": "patient_medication",
  "patient_id": "patient_001",
  "visit_id": "patvisit_001",
  "medicine_id": "med_001",
  "quantity": "1",
  "startdate": "20180101",
  "enddate": "20180201"
}

{
  "_id": "patvisit_001",
  "_rev": "1-f071319f39f4f5df2a1e8214ccc46453",
  "doctype": "patientvisit",
  "patient_id": "patient_001",
  "visitdate": "20010910",
  "complaint": "fever",
  "doctor_attended": "doctor_001",
  "feespaid": "300"
}

{
  "_id": "med_001",
  "_rev": "1-030b39b7773de39fb6a178d77d3d6461",
  "doctype": "medicine",
  "batch": "001",
  "batchdate": "20010910",
  "expirydate": "20021010",
  "unit": "bottle",
  "type": "pills",
  "vendor": "xyz",
  "curr_stock": "20",
  "cost": "200",
  "lastreplenished": "20180101"
}

{
  "_id": "patient_001",
  "_rev": "1-1772d5ff54c46b15642d0b10e5f3906d",
  "doctype": "patient",
  "FirstName": "Test",
  "LastName": "Patient3",
  "Gender": "Male",
  "DOB": "19401010",
  "Address1": "999, XYZ Street",
  "Address2": "ABC avenue",
  "City": "London",
  "Pincode": "AB1CD2",
  "State": "BlahBlah",
  "Country": "UK",
  "Mobile": "001182919112"
}

是否可以通过传递patient_id在PouchDB中查询以从其他文档获取相关信息,类似于RDBMS中的联接? (即

SELECT PatMed.*, PatVisit.*, Patient.* from Patient P
INNER JOIN PatientVisit PatVisit on P.ID = PatVisit.Patient_ID
INNER JOIN PatientMed PatMed on PatVisit.ID = PatMed.VisitID
...

2 个答案:

答案 0 :(得分:2)

我对RDBMS不太熟悉,我不确定是否收到您的问题,但您可以创建如下索引。

var myIndex = {
  _id: '_design/my_index',
  views: {
    'my_index': {
      map: function (doc) {
        if(doc.patient_id){
          // emit patient_id as key and doc as value
          emit(doc.patient_id, doc);
        }
      }.toString()
    }
  }
};

现在您可以PUT查询上述索引,找到patient_id字段等于某个值的所有文档:

// save the design doc view and the corresponding index
pouch.put(myIndex).then(() => {
  // query the index
  return pouch.query('my_index', {key: 'patient_001'});
}).then(result => {
  // found docs with patient_id === 'patient_001'
  console.log(result)
});

答案 1 :(得分:-1)

我使用了user3405291建议的方法,并获得了所有具有patient_id的文档。

基于doctype,然后我将项目推送到差异数组并使用js库JinqJS,我加入它们以获取所需的数据。

谢谢!