本文介绍了需要建议以mongoose在mongodb中设计数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个馆藏:医院和患者.本月患者在医院A进行检查,但是下个月,该患者在医院B进行检查.那么在更新患者信息时,如何保存患者已经在医院A进行检查的患者病史?请给我个建议吗?谢谢

I have 2 collections: Hospital and Patient.This month the patient is examination at hospital A, But next month, this patient is examination at hospital B. So when updating patient information, how to save patient's history that patient is already examination at hospital A? Please give me an advice? Thanks

推荐答案

您需要为此准备一个单独的检查收藏.(就像关系数据库中的中间(关联)表一样.)

You need to have a separate examination collection for this. (It is like the intermediate (associative) table in relational databases.)

解决此问题的一种方法是使用虚拟填充.使用虚拟填充,我们不需要保留对考试的引用,这将简化添加,更新或删除考试时的操作.因为只需要更新考试集.

One way to solve this is using virtual populate.With virtual populate we don't need to keep references to the examinations, which will simplify things when an examination is added, updated or deleted.Because only the examinations collection will need to be updated.

patient.js

patient.js

const mongoose = require("mongoose");

const patientSchema = new mongoose.Schema(
  {
    name: String
  },
  {
    toJSON: { virtuals: true }
  }
);

// Virtual populate
patientSchema.virtual("examinations", {
  ref: "Examination",
  foreignField: "patientId",
  localField: "_id"
});

module.exports = mongoose.model("Patient", patientSchema);

hospital.js

hospital.js

const mongoose = require("mongoose");

const hospitalSchema = new mongoose.Schema(
  {
    name: String
  },
  {
    toJSON: { virtuals: true }
  }
);

// Virtual populate
hospitalSchema.virtual("examinations", {
  ref: "Examination",
  foreignField: "hospitalId",
  localField: "_id"
});

module.exports = mongoose.model("Hospital", hospitalSchema);

examination.js

examination.js

const mongoose = require("mongoose");

const examinationSchema = new mongoose.Schema({
  when: {
    type: Date,
    default: Date.now()
  },
  patientId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Patient"
  },
  hospitalId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Hospital"
  }
});

module.exports = mongoose.model("Examination", examinationSchema);

如您所见,我们的患者和医院架构非常干净,没有任何检查参考.

As you see our patient and hospital schemas are very clean without any examination reference.

让我们现有这些患者.

{
    "_id" : ObjectId("5e0f86d0ea3eb831a4845064"),
    "name" : "Patient 1",
    "__v" : NumberInt(0)
},
{
    "_id" : ObjectId("5e0f86dbea3eb831a4845065"),
    "name" : "Patient 2",
    "__v" : NumberInt(0)
}

让我们拥有这些现有的医院.

Let's have these existing hospitals.

{
    "_id" : ObjectId("5e0f86feea3eb831a4845066"),
    "name" : "Hospital 1",
    "__v" : NumberInt(0)
},
{
    "_id" : ObjectId("5e0f8705ea3eb831a4845067"),
    "name" : "Hospital 2",
    "__v" : NumberInt(0)
}

让我们进行这些现有的考试.

Let's have these existing examinations.

/* Patient 1 - Hospital 1 */
{
    "when": "2020-01-03T18:27:12.997Z",
    "_id": "5e0f878346e50d41d846d482",
    "patientId": "5e0f86d0ea3eb831a4845064",
    "hospitalId": "5e0f86feea3eb831a4845066",
    "__v": 0
},
/* Patient 1 - Hospital 1 */
{
    "when": "2020-01-03T18:27:12.997Z",
    "_id": "5e0f87a646e50d41d846d483",
    "patientId": "5e0f86d0ea3eb831a4845064",
    "hospitalId": "5e0f86feea3eb831a4845066",
    "__v": 0
},
/* Patient 1 - Hospital 2*/
{
    "when": "2020-01-03T18:27:12.997Z",
    "_id": "5e0f87c446e50d41d846d484",
    "patientId": "5e0f86d0ea3eb831a4845064",
    "hospitalId": "5e0f8705ea3eb831a4845067",
    "__v": 0
},
/* Patient 2 - Hospital 1 */
{
    "when": "2020-01-03T18:27:12.997Z",
    "_id": "5e0f87e046e50d41d846d485",
    "patientId": "5e0f86dbea3eb831a4845065",
    "hospitalId": "5e0f86feea3eb831a4845066",
    "__v": 0
}

现在,如果我们想获取患者及其检查的信息,可以使用以下代码:

Now if we want to get the info of a patient and his/her examinations we can use the following code:

app.get("/patients/:id", async (req, res) => {
  const result = await Patient.findById(req.params.id).populate("examinations");
  res.send(result);
});

结果将是这样的:

{
    "_id": "5e0f86d0ea3eb831a4845064",
    "name": "Patient 1",
    "__v": 0,
    "examinations": [
        {
            "when": "2020-01-03T18:27:12.997Z",
            "_id": "5e0f878346e50d41d846d482",
            "patientId": "5e0f86d0ea3eb831a4845064",
            "hospitalId": "5e0f86feea3eb831a4845066",
            "__v": 0
        },
        {
            "when": "2020-01-03T18:27:12.997Z",
            "_id": "5e0f87a646e50d41d846d483",
            "patientId": "5e0f86d0ea3eb831a4845064",
            "hospitalId": "5e0f86feea3eb831a4845066",
            "__v": 0
        },
        {
            "when": "2020-01-03T18:27:12.997Z",
            "_id": "5e0f87c446e50d41d846d484",
            "patientId": "5e0f86d0ea3eb831a4845064",
            "hospitalId": "5e0f8705ea3eb831a4845067",
            "__v": 0
        }
    ],
    "id": "5e0f86d0ea3eb831a4845064"
}

我们甚至可以用内部填充物来填充这样的医院:

We can even populate the hospital like this with an inner populate:

app.get("/patients/:id", async (req, res) => {
  const result = await Patient.findById(req.params.id).populate({
    path: "examinations",
    populate: {
      path: "hospitalId"
    }
  });

  res.send(result);
});

结果将包含医院信息:

{
    "_id": "5e0f86d0ea3eb831a4845064",
    "name": "Patient 1",
    "__v": 0,
    "examinations": [
        {
            "when": "2020-01-03T18:27:12.997Z",
            "_id": "5e0f878346e50d41d846d482",
            "patientId": "5e0f86d0ea3eb831a4845064",
            "hospitalId": {
                "_id": "5e0f86feea3eb831a4845066",
                "name": "Hospital 1",
                "__v": 0,
                "id": "5e0f86feea3eb831a4845066"
            },
            "__v": 0
        },
        {
            "when": "2020-01-03T18:27:12.997Z",
            "_id": "5e0f87a646e50d41d846d483",
            "patientId": "5e0f86d0ea3eb831a4845064",
            "hospitalId": {
                "_id": "5e0f86feea3eb831a4845066",
                "name": "Hospital 1",
                "__v": 0,
                "id": "5e0f86feea3eb831a4845066"
            },
            "__v": 0
        },
        {
            "when": "2020-01-03T18:27:12.997Z",
            "_id": "5e0f87c446e50d41d846d484",
            "patientId": "5e0f86d0ea3eb831a4845064",
            "hospitalId": {
                "_id": "5e0f8705ea3eb831a4845067",
                "name": "Hospital 2",
                "__v": 0,
                "id": "5e0f8705ea3eb831a4845067"
            },
            "__v": 0
        }
    ],
    "id": "5e0f86d0ea3eb831a4845064"
}

现在,有了这些知识,您就可以自己从医院侧实施检索操作了.

Now with this knowledge you can yourself implement the retrieve operations from the hospital side.

这篇关于需要建议以mongoose在mongodb中设计数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 22:32