pouchdb - 在 PouchDB 中加入(链接)文档

标签 pouchdb

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

我有 4 种不同类型的文档都存储在一个数据库中,如上所示:

{
  "_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
...

最佳答案

我对 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)
});

关于pouchdb - 在 PouchDB 中加入(链接)文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49571935/

相关文章:

couchdb - 智能代理在 CouchDB 上提供过滤 View

database - CouchDB - 同步是否将数据库复制给所有用户?

python - 将 pouchdb 与 python 一起使用

couchdb - 如何查询 pouchdb 中已删除的文档?

javascript - 将多个 PouchDB 数据库同步到一个 CouchDB 数据库

synchronization - 具有共享数据可扩展性的 CouchDB db-per-user

ubuntu - PouchDB - 尝试连接到 CouchDB 时为 "Database encountered an unknown error"

couchdb - PouchDB 离线时保持轮询

apache - PouchDB View 按键查找

pouchdb - PouchDB 的顺序唯一 ID