【发布时间】:2015-10-06 14:51:41
【问题描述】:
假设 StrongLoop (https://docs.strongloop.com/display/public/LB/HasManyThrough+relations) 使用的医生和患者的标准示例:
common/models/physician.json
{
"name": "Physician",
"base": "PersistedModel",
"properties": {
"name": {
"type": "string"
}
},
"validations": [],
"relations": {
"patients": {
"type": "hasMany",
"model": "Patient",
"foreignKey": "patientId",
"through": "Appointment"
},
common/models/patient.json
{
"name": "Patient",
"base": "PersistedModel",
"properties": {
"name": {
"type": "string"
}
},
"validations": [],
"relations": {
"physicans": {
"type": "hasMany",
"model": "Physician",
"foreignKey": "physicianId",
"through": "Appointment"
},
common/models/appointment.json
{
"name": "Appointment",
"base": "PersistedModel",
"properties": {
"appointmentDate": {
"type": "date"
}
},
"validations": [],
"relations": {
"physician": {
"type": "belongsTo",
"model": "Physician",
"foreignKey": "physicianId"
},
"patient": {
"type": "belongsTo",
"model": "Patient",
"foreignKey": "patientId"
},
如何通过约会日期从医生筛选中获取患者?
我尝试使用这样的东西:
Physician.findOne({
"where": {"id": physId},
"include": {
"relation": "patients",
"scope": {
"where": {
"appointmentDate": { "gt": fixedDate }
}
}
}
}, callback);
但似乎 Loopback 在 Patient 模型上而不是在 Appointment 模型上寻找约会日期。
一些有用的提示? 谢谢!
【问题讨论】:
-
你应该看看 mongoose :) mongoosejs.com 还要注意 MongoDB 内部的关系并不理想,所以你可能需要重新考虑你的数据库模型 :)
标签: mongodb filter has-many-through loopbackjs strongloop