我有两个型号。一份用于患者,另一份用于实验室测试报告。在报告模型中插入记录时,我通过使用公共字段 contactno 获取患者 ID,并将其存储在字段患者的报告模型中,该字段引用患者模型的 ObjectId。
在患者模型中,有一系列报告,填充后应显示与患者相关的所有报告。以下是我的模型和控制器。
**患者模型 **
const mongoose = require("mongoose")
const Reports = require('./reportModel')
const patientSchema = mongoose.Schema({
name:{
type:String,
required:[true,"Please Enter Patient Name"]
},
gender:{
type:String,
required:[true, "Please Enter Patient Gender"]
},
age:{
type:Number,
required:[true, "Please Enter Patient Age"]
},
address:{
type:String,
required:[true, "Please Enter Patient Adress"]
},
dateOfBirth:{
type:String,
required:[true, "Please Enter Patient Date of Birth"]
},
email:{
type:String,
required:[true, "Please Enter Patient Email"]
},
contact:{
type:Number,
required:[true, "Please Enter Patient contact"]
},
reports: [{
type: mongoose.Schema.ObjectId,
ref: "Report",
}],
emergContact:{
type:Number,
required:[true, "Please Enter Patient Emergency Contact"]
},
weight:{
type:Number,
default:1
},
reviews: [
{
user: {
type: mongoose.Schema.ObjectId,
ref: "User",
required: true,
},
name: {
type: String,
required: true,
},
rating: {
type: Number,
required: true,
},
comment: {
type: String,
required: true,
},
},
],
user: {
type: mongoose.Schema.ObjectId,
ref: "User",
required: true,
},
createdAt:{
type:Date,
default:Date.now
}
})
module.exports = mongoose.model("Patient",patientSchema)
**报告模型 **
const mongoose = require("mongoose");
const validator = require("validator");
const reportSchema = mongoose.Schema({
patient: {
type: mongoose.Schema.ObjectId,
ref: "Patient",
required: true,
},
type:{
type: String,
required: [true, "Please Enter Your testType"],
},
name:{
type:String,
required:[true, "Please enter Type of test"]
},
contact:{
type:Number,
required:[true, "Must enter valid contact number of patient"]
},
remarks:{
type:String,
}
})
module.exports = mongoose.model("Report", reportSchema);
下面是报告控制器,它在报告模型中保存数据,并通过查询电话号码来存储患者的对象ID
exports.createReport = catchAsyncErrors(async(req,res,next)=> {
const patient = await Patient.findOne({contact:req.body.contact})
const newreq = {patient:patient._id,
type:req.body.type,
name:req.body.name,
contact:req.body.contact,
remarks:req.body.remarks
}
const report = await Report.create(newreq)
res.status(200).json({
success:true,
report
})
}
)
报告查询的输出
{
"success": true,
"report": {
"_id": "652dc1e07acd6e401e4d3da5",
"patient": "651e3fa49b84a8a8c8532fa1",
"type": "Skin Test",
"name": "Skin Examination",
"contact": 532531216,
"remarks": "Should use sun block",
"__v": 0
}
}
单个患者查询
exports.getPatientDetails = catchAsyncErrors(
async(req,res,next)=>{
const patient = await Patient.findById(req.params.id).populate({path:'Report',strictPopulate:false}).exec()
if(!patient){
return next(new ErrorHandler("Patient Not Found",404))
}
res.status(200).json({
success:true,
patient
})
}
)
{
"success": true,
"patient": {
"reports": [],
"_id": "651e3fa49b84a8a8c8532fa1",
"name": "Patient 3",
"gender": "Male",
"age": 65,
"address": "Shahdara Lahore",
"dateOfBirth": "07-05-1945",
"email": "[email protected]",
"contact": 532531216,
"emergContact": 55666666,
"weight": 90,
`"
createdAt": "2023-10-05T04:46:28.810Z",
"__v": 0,
"reviews": []
}
}
报告数组为空,而根据我的理解,它应该显示与指定患者相关的报告数组。
我尝试在线搜索有关 mongodb 表连接和填充的信息。
我现在粘贴两个控制器的代码以对其他人有所帮助。我通过进行下面提到的更改解决了这个问题。
报告控制器
exports.createReport = catchAsyncErrors(async (req, res, next) => {
const patient = await Patient.findOne({ contact: req.body.contact })
const newreq = {
patient: patient._id,
type: req.body.type,
name: req.body.name,
contact: req.body.contact,
remarks: req.body.remarks
}
const report = await Report.create(newreq)
newpatient = await Patient.findOneAndUpdate(
{ _id: report.patient },
{ runValidators: false, context: 'query' },
)
await newpatient.reports.push(report._id)
await newpatient.save()
res.status(200).json({
success: true,
report
})
})
患者控制器
exports.getPatientDetails = catchAsyncErrors(async (req, res, next) => {
let patient = await Patient.findById(req.params.id).populate(
{ path: 'reports', strictPopulate: false }
)
if (!patient) {
return next(new ErrorHandler("Patient Not Found",404))
}
res.status(200).json({
success: true,
patient
})
})
现在,通过使用联系号码创建每个新报告,它都会在两侧填充 ObjectId,并且当访问患者时,它还会检索关联的报告。