我正在使用loopback 3来构建mongoDB的后端。 所以我有 3 个模型:对象、附件和 AwsS3。
- 对象与 Embedding2Many 与 Attachment 的关系。
- 附件与 AwsS3 有关系 Many2One。
对象在 mongoDB 中看起来像这样
[
{
"fieldA": "valueA1",
"attachments": [
{
"id": 1,
"awsS3Id": "1234"
},
{
"id": 2,
"awsS3Id": "1235"
}
]
},
{
"fieldA": "valueA2",
"attachments": [
{
"id": 4,
"awsS3Id": "1236"
},
{
"id": 5,
"awsS3Id": "1237"
}
]
}
]
AwsS3在mongoDB中看起来像这样
。[
{
"id": "1",
"url": "abc.com/1"
},
{
"id": "2",
"url": "abc.com/2"
}
]
问题是:如何通过 RestAPI 获取包含附件和 AwsS3.url 的对象?
我尝试过使用include
和scope
过滤器。但它没有用。看起来,这个函数没有在环回3中实现,对吧?这是我对 GET 请求尝试的内容:
{
"filter": {
"include": {
"relation": "Attachment",
"scope": {
"include": {
"relation": "awsS3",
}
}
}
}
}
有了这个请求,我只得到了带有附件的对象,而没有来自 AwsS3 的任何内容。
关系定义的更新
从Object
到Attachment
的关系:
"Attachment": {
"type": "embedsMany",
"model": "Attachment",
"property": "attachments",
"options": {
"validate": true,
"forceId": false
}
},
从Attachment
到 AwsS3 的关系
在附件中.json
"relations": {
"awsS3": {
"type": "belongsTo",
"model": "AwsS3",
"foreignKey": ""
}
}
在 AwsS3.json 中
"relations": {
"attachments": {
"type": "hasMany",
"model": "Attachment",
"foreignKey": ""
}
}
试试这个过滤器:
{ "filter": { "include": ["awsS3", "attachments"]}}}}