我有两个模型。一个是病人,另一个是实验室测试报告。在报告模型中插入记录时,我使用通用字段contactno获取患者ID,并将其存储在报告模型中的字段patient中,该字段patient引用患者模型的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表连接和填充的信息。
1条答案
按热度按时间fkaflof61#
好吧,我现在粘贴两个控制器的代码,以帮助他人。我通过下面提到的修改解决了这个问题。
报表控制器
患者控制器
现在,在每次使用联系人号码创建新报告时,它都会在两侧填充ObjectId,并且在访问患者时,它还会检索相关报告。