【问题标题】:Join in mongodb using nodejs使用nodejs加入mongodb
【发布时间】:2016-07-27 19:26:17
【问题描述】:

我的病人模型是这样的:

PatientModel=new Schema({
   "PatientID":{type:String},
   "PatientGender":{type:String},
   "PatientDateOfBirth":{type:Date},
   "PatientRace":{type:String},
   "PatientMaritalStatus":{type:String},
   "PatientLanguage":{type:String},
   "PatientPopulationPercentageBelowPoverty":{type:Number},
   "FirstName":{type:String},
   "LastName":{type:String},
   "City":{type:String}
});

疾病模型

DiseaseModel=new Schema({
   "PatientID":{type:String},
   "AdmissionID":{type:Number},
   "PrimaryDiagnosisCode":{type:String},
   "Disease":{type:String}
});

我想按 “City”(在 PatientModel 中)排列具有特定 “Diseases”(在 DiseaseModel 中)组的患者计数降序排列,最多限制10个城市。如何在 nodejs 中编写查询?

疾病样本数据:

{
"PatientID": "170EF37F-1098-462C-838F-C56C5455CB98",
"AdmissionID": 3,
"PrimaryDiagnosisCode": "C00.1",
"PrimaryDiagnosisDescription": "Malignant neoplasm of external lower lip"
 }

患者的样本数据:

{
"PatientID": "170EF37F-1098-462C-838F-C56C5455CB98",
"PatientGender": "Male",
"PatientDateOfBirth": "1951-09-26 08:48:36.700",
"PatientRace": "White",
"PatientMaritalStatus": "Married",
"PatientLanguage": "English",
"PatientPopulationPercentageBelowPoverty": 14.15,
"FirstName": "BENJAMIN",
"LastName": "YIN",
"City": "Gokak",
}

【问题讨论】:

  • 按什么降序排列?疾病名称?
  • @SiddharthAjmera 患者数量的降序。
  • 您能否为您的PatientDisease 收藏提供一些示例数据?
  • 我添加了有问题的示例数据。 @SiddharthAjmera

标签: node.js mongodb mongoose


【解决方案1】:

MongoDB 不支持连接。所以用这个模型,我认为是做不到的。不过,您可以做的是,在您的 PatientModel 中添加一个 diseases 字段,这将是一个字符串数组。像这样的:

PatientModel=new Schema({
    "PatientID":{type:String},
    "PatientGender":{type:String},
    "PatientDateOfBirth":{type:Date},
    "PatientRace":{type:String},
    "PatientMaritalStatus":{type:String},
    "PatientLanguage":{type:String},
    "PatientPopulationPercentageBelowPoverty":{type:Number},
    "FirstName":{type:String},
    "LastName":{type:String},
    "City":{type:String},
    "Diseases": { type: [String] }
});

然后你可以简单地使用这个聚合查询。这是针对 Mongo Shell 的。但是这样的 Mongoose 等效项会起作用。

db.patients.aggregate([
    { $match: { Diseases: your_specified_disease } },
    { $group: {
                  _id: { city: "$City" },
                  patients_count: { $sum : 1 }
              } },
    { $sort: { patients_count: -1 } },
    { $limit: 10 }
]);

我刚刚测试过。它正在工作。

编辑: 如果您不愿意更改架构,那么按照 Neil 的建议,您可以使用 $lookup。在这种情况下,您的查询应如下所示:

db.diseases.aggregate([
    { $match: { Disease: your_specified_disease } },
    { $lookup:  {
                    from: "patients",
                    localField: "PatientID",
                    foreignField: "PatientID",
                    as: "Patient"
            }
    },
    { $group: {
                  _id: { city: "$Patient.City" },
                  patients_count: { $sum : 1 }
              } },
    { $sort: { patients_count: -1 } },
    { $limit: 10 }
])

我没有测试过这个,因为我没有你拥有的所有数据。但是请测试一下,让我知道它是否适合你。

【讨论】:

  • @Shubham 如果你有 MongoDB 3.2,总是有$lookup,或者当然只要“相关”_id 存储在其他集合中,你可以关注"Mongoose - How to group by and populate?"
  • 假设我首先对疾病集合进行查询并获取所有患有疾病的患者:“A”,并且使用此患者数据可以查询患者集合吗?这意味着我将查询数据库 2 次...
  • 是的,你可以。然后您必须使用$in 查询PatientID。这将使您的查询逻辑更加复杂。这是一个简单的建议。
  • 请给我一些时间。我会回到这个。
  • 如果我使用 $in 那么查询会是什么样子?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-28
  • 1970-01-01
  • 2017-03-02
相关资源
最近更新 更多