简体   繁体   中英

How to query multiple MongoDB collections and NodeJS?

I have three collections in MongoDB.

In Drug JSON, using MEDID get its respective CMIDs from Composition JSON. Using that CMIDs get its respective chemical Names from the Chemicals JSON, then stores in one array with their medid , cmid and its name .

I'm using NodeJS. Please help me, thanks in advance.

DRUG collection

{ 
   "MEDID":"AAA001-01",
   "BRANDNAME":"TASULIN D",
   "MEDTYPECODE":"CAP",
   "DOSE":"",
   "DOSEUNIT":"",
   "CHEMICAL1":"TAMSULSIN HYDROCHLORIDE",
   "CHEMICAL2":"DUTASTERIDE",
   "CHEMICAL3":"",
   "CHEMICAL4":"",
   "CHEMICAL5":"",
   "CHEMICAL6":"",
   "CHEMICAL7":"",
   "CHEMICAL8":"",
   "CHEMICAL9":"",
   "CHEMICAL10":"",
   "CHEMICAL11":"",
   "SPECIALITY":"GEN",
   "MANUFACTURER":"IPCA",
   "MFTID":"xyz123"
}

Compostion Collection

{"MEDID":"AAA001-01","CMID":"ABC001"},
{"MEDID":"AAA001-01","CMID":"ABC002"}

Chemical Collection

{"CMID":"ABC001","Name":"TAMSULSIN HYDROCHLORIDE"},
{"CMID":"ABC002","Name":"DUTASTERIDE"},
db.drug.aggregate([
   {
     $lookup:
       {
         from: "composition",
         localField: "medid",
         foreignField: "medid",
         as: "composition"
       }
  },
    $lookup:
       {
         from: "chemical",
         localField: "composition.cmid",
         foreignField: "cmid",
         as: "chemical"
       }
   },
    $group: {
        _id: "$medid",
        cmid: { $first: "$composition.cmid" },
        name: { $first: "$chemical.name" }
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM