您需要为此准备单独的检查集。(就像关系数据库中的中间(关联)表一样。)
解决此问题的一种方法是使用虚拟填充。使用虚拟填充,我们无需保留对考试的引用,这将简化添加,更新或删除考试时的操作。因为只需要更新考试集。
Patient.js
const mongoose = require("mongoose");const patientSchema = new mongoose.Schema( { name: String }, { toJSON: { virtuals: true } });// Virtual populatepatientSchema.virtual("examinations", { ref: "Examination", foreignField: "patientId", localField: "_id"});module.exports = mongoose.model("Patient", patientSchema);hospital.js
const mongoose = require("mongoose");const hospitalSchema = new mongoose.Schema( { name: String }, { toJSON: { virtuals: true } });// Virtual populatehospitalSchema.virtual("examinations", { ref: "Examination", foreignField: "hospitalId", localField: "_id"});module.exports = mongoose.model("Hospital", hospitalSchema);inspection.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);如您所见,我们的患者和医院架构非常干净,没有任何检查参考。
让我们来看看这些现有的病人。
{ "_id" : ObjectId("5e0f86d0ea3eb831a4845064"), "name" : "Patient 1", "__v" : NumberInt(0)},{ "_id" : ObjectId("5e0f86dbea3eb831a4845065"), "name" : "Patient 2", "__v" : NumberInt(0)}让我们拥有这些现有的医院。
{ "_id" : ObjectId("5e0f86feea3eb831a4845066"), "name" : "Hospital 1", "__v" : NumberInt(0)},{ "_id" : ObjectId("5e0f8705ea3eb831a4845067"), "name" : "Hospital 2", "__v" : NumberInt(0)}让我们进行这些现有的检查。
{ "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},{ "when": "2020-01-03T18:27:12.997Z", "_id": "5e0f87e046e50d41d846d485", "patientId": "5e0f86dbea3eb831a4845065", "hospitalId": "5e0f86feea3eb831a4845066", "__v": 0}现在,如果我们要获取患者及其检查的信息,可以使用以下代码:
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"}我们甚至可以用内部填充来填充这样的医院:
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"}现在,有了这些知识,您就可以自己从医院侧实施检索操作。



