mongodb - 在 mongodb 查询期间加载嵌套虚拟
问题描述
我不熟悉使用 ObjectId 以外的键来链接来自其他集合的数据。目前,我与其他各种数据进行了约会,我想引入这些数据,这样我就可以评估付款是否到期。
我的查询有效,但它没有为每个患者带来计划信息。我知道它对每个填充进行单独的查询,因此在填充患者信息后我必须这样做populate('patientID')
:
const appts = await Appt.find(searchParams)
.populate('patientID')
.populate('patientID.plan')
.populate('status')
.populate('type')
.sort({ scheduled: -1 });
以上不适用于引入计划信息的嵌套 JSON,但它适用于引入患者集合、状态和类型。只有 patientID.plan 填充不起作用。
我的模式:
const familySchema = new mongoose.Schema({
ID: {
type: Number,
index: true
},
family: String
});
const paymentplanSchema = new mongoose.Schema({
ID: {
type: Number,
index: true
},
plan: String,
planamt: Number
});
const patientSchema = new mongoose.Schema({
ID: {
type: Number
},
familyID: Number,
first: String,
last: String,
careplanID: Number,
otherData: variousTypes
});
patientSchema.virtual('plan', {
ref: 'PaymentPlan', // The model to use
localField: 'careplanID', // Find people where `localField`
foreignField: 'ID' // is equal to `foreignField`
});
patientSchema.pre('find', function() {
this.populate('plan');
});
const typeSchema = new mongoose.Schema({
ID: Number,
appttype: String,
abbr: String,
amt: Number,
code: String,
length: Number
});
const statusSchema = new mongoose.Schema({
ID: Number,
status: String
});
const apptSchema = new mongoose.Schema({
ID: Number,
patientID: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Patient'
},
oldPatientID: Number,
status: {
type: mongoose.Schema.Types.ObjectId,
ref: 'ApptStatus'
},
type: {
type: mongoose.Schema.Types.ObjectId,
ref: 'ApptType'
},
scheduled: Date,
note: String
});
mongoose.model('Appt', apptSchema);
mongoose.model('ApptStatus', statusSchema);
mongoose.model('ApptType', typeSchema);
mongoose.model('Patient', patientSchema);
mongoose.model('PaymentPlan', paymentplanSchema);
如何让患者数据与计划数据一起加载?我不明白我做错了什么,而且我还有其他想以这种方式连接的东西(通过索引而不是 ObjectId),但只是不明白我做错了什么。
更新以添加更多详细信息: 我在后端获取约会的查询是这样的:
module.exports.search = async (req, res) => {
console.log('GET the appts');
const searchParams =
req.params.query === 'today'
? { scheduled: { $gt: new Date(dayStart), $lt: new Date(dayEnd) } }
: req.body;
console.log(searchParams);
try {
const appts = await Appt.find(searchParams)
.populate({
path: 'patientID',
populate: { path: 'plan' }
})
.populate('status')
.populate('type')
.sort({ scheduled: -1 });
if (!appts) {
console.log(`No appointments found`);
}
appts.forEach(p => {
const patient = p.patientID ? p.patientID.nickname : 'NONE';
const plan =
p.patientID && p.patientID.plan ? p.patientID.plan.planamt : 0;
console.log(patient, plan);
});
console.log(appts.length, 'appts found');
res.send(appts);
} catch (err) {
console.log(`Error`, err);
return res.status(500).send(err);
}
};
在控制台中,它正在正确记录(示例):
CarF 60
8075 'appts found'
在前端,所有对象都被填充,除了患者 ID.plan。patientID 对象不包括任何条目上的计划字段。patientID、status 和 type 都填充了相应的对象。
为什么此日志记录在后端,但在前端不可见?
解决方案
您应该可以通过将路径选项传递给populate()
:
const appts = await Appt.find(searchParams)
.populate('patientID')
.populate({
path: 'patientID',
populate: {path: 'plan'}
})
.populate('status')
.populate('type')
.sort({ scheduled: -1 });
请参阅官方文档中的https://mongoosejs.com/docs/populate.html#deep-populate
推荐阅读
- regex - 从已捕获的组正则表达式中删除字符串
- python - 关于拆分(Python函数)
- c++ - 不允许不完整的类型(指针)
- php - 使用 PHP 隐藏金额 - Pinpayments
- php - php echo方法多个数组键值
- php - 如何将laravel查询列转换为数组并与变量数组laravel匹配
- c - 无法将特定字符添加到 char* 的开头,它只允许我在 C 之后添加它?
- ios - GoogleMaps 作为间接依赖导致构建错误
- javascript - 基于用户提示的 Javascript 并行数组问题。如何停止在循环中超出数组范围?
- xcode - SwiftUI:根据一年中的日期显示图像