2016-12-06 67 views
1

我有這樣的文件名爲診斷集合中的一個陣列內加入數據:MongoDB的對象

{ 
     "_id" : ObjectId("582d43d18ec3f432f3260682"), 
     "patientid" : ObjectId("582aacff3894c3afd7ad4677"), 
     "doctorid" : ObjectId("582a80c93894c3afd7ad4675"), 
     "medicalcondition" : "high fever, cough, runny nose.", 
     "diagnosis" : "Viral Flu", 
     "addmissiondate" : "2016-01-12", 
     "dischargedate" : "2016-01-16", 
     "bhtno" : "125", 
     "prescription" : [ 
      { 
       "drug" : ObjectId("58345e0e996d340bd8126149"), 
       "instructions" : "Take 2 daily, after meals." 
      }, 
      { 
       "drug" : ObjectId("5836bc0b291918eb42966320"), 
       "instructions" : "Take 1 daily, after meals." 
      } 
     ] 
    } 

藥物 ID處方對象數組裏面是叫藥物一個單獨的集合,請參閱下面的示例文檔:

{ 
    "_id" : ObjectId("58345e0e996d340bd8126149"), 
    "genericname" : "Paracetamol Tab 500mg", 
    "type" : "X", 
    "isbrand" : false 
} 

我想創建一個使用本機node.js驅動程序來獲得結果的mongodb查詢像這樣:

 { 
       "_id" : ObjectId("582d43d18ec3f432f3260682"), 
       "patientid" : ObjectId("582aacff3894c3afd7ad4677"), 
       "doctorid" : ObjectId("582a80c93894c3afd7ad4675"), 
       "medicalcondition" : "high fever, cough, runny nose.", 
       "diagnosis" : "Viral Flu", 
       "addmissiondate" : "2016-01-12", 
       "dischargedate" : "2016-01-16", 
       "bhtno" : "125", 
       "prescription" : [ 
        { 
         "drug" : 
         { 
          "_id" : ObjectId("58345e0e996d340bd8126149"), 
          "genericname" : "Paracetamol Tab 500mg", 
          "type" : "X", 
          "isbrand" : false 
         }, 
         "instructions" : "Take 2 daily, after meals." 
        }, 
        ... 
       ] 
      } 

任何關於如何處理類似結果的建議非常感謝,謝謝。

回答

0

使用聚合框架,您可以創建一個管道,首先壓平使用$unwind運營商和$lookup後續的流水線步驟做了「左外連接」上的「藥」集合prescription陣列。對「已連接」字段創建的數組應用另一個$unwind操作。 $group以前從第一個管道中展開的文檔,其中$unwind運算符爲處方數組中的每個元素輸出文檔。

組裝上述管道,運行以下骨料操作:

db.diagnoses.aggregate([ 
    { 
     "$project": {    
      "patientid": 1, 
      "doctorid": 1, 
      "medicalcondition": 1, 
      "diagnosis": 1, 
      "addmissiondate": 1, 
      "dischargedate": 1, 
      "bhtno": 1, 
      "prescription": { "$ifNull" : [ "$prescription", [ ] ] } 
     } 
    }, 
    { 
     "$unwind": { 
      "path": "$prescription", 
      "preserveNullAndEmptyArrays": true 
     } 
    },  
    { 
     "$lookup": { 
      "from": "drugs", 
      "localField": "prescription.drug", 
      "foreignField": "_id", 
      "as": "prescription.drug" 
     } 
    }, 
    { "$unwind": "$prescription.drug" }, 
    { 
     "$group": { 
      "_id": "$_id", 
      "patientid" : { "$first": "$patientid" }, 
      "doctorid" : { "$first": "$doctorid" }, 
      "medicalcondition" : { "$first": "$medicalcondition" }, 
      "diagnosis" : { "$first": "$diagnosis" }, 
      "addmissiondate" : { "$first": "$addmissiondate" }, 
      "dischargedate" : { "$first": "$dischargedate" }, 
      "bhtno" : { "$first": "$bhtno" }, 
      "prescription" : { "$push": "$prescription" } 
     } 
    } 
]) 

樣本輸出

{ 
    "_id" : ObjectId("582d43d18ec3f432f3260682"), 
    "patientid" : ObjectId("582aacff3894c3afd7ad4677"), 
    "doctorid" : ObjectId("582a80c93894c3afd7ad4675"), 
    "medicalcondition" : "high fever, cough, runny nose.", 
    "diagnosis" : "Viral Flu", 
    "addmissiondate" : "2016-01-12", 
    "dischargedate" : "2016-01-16", 
    "bhtno" : "125", 
    "prescription" : [ 
     { 
      "drug" : { 
       "_id" : ObjectId("58345e0e996d340bd8126149"), 
       "genericname" : "Paracetamol Tab 500mg", 
       "type" : "X", 
       "isbrand" : false 
      }, 
      "instructions" : "Take 2 daily, after meals." 
     }, 
     { 
      "drug" : { 
       "_id" : ObjectId("5836bc0b291918eb42966320"), 
       "genericname" : "Paracetamol Tab 100mg", 
       "type" : "Y", 
       "isbrand" : false 
      }, 
      "instructions" : "Take 1 daily, after meals." 
     } 
    ] 
} 
+0

我遇到了一個小問題就與此查詢,如果沒有處方數據的含義該數組不在文件中,查詢不會返回任何內容,是否有某種方法可以使其像左連接一樣工作? – VindulaF

+1

@VindulaFernando用一種方法更新了答案。請檢查。 – chridam

+0

仍然得到同樣的結果我的診斷文件看起來像這樣在那種情況下 '{ 「_id」:物件( 「582d43d18ec3f432f3260682」), 「patientid」:物件( 「582aacff3894c3afd7ad4677」), 「doctorid」:物件(」 「addmissiondate」:「2016-01-12」, 「已排出」:「2016年12月12日」 -01-16「, 」bhtno「:」125「 }' – VindulaF