【问题标题】:Loopback auto relation include filter not working环回自动关系包括过滤器不起作用
【发布时间】:2026-01-25 21:20:03
【问题描述】:

我有一个名为 category 的模型,它可以有多个子类别并且属于一个类别,因此,它是一个与汽车相关的模型。所以我在我的数据库中插入了一些(我正在使用 MongoDB),我想检索我所有不属于任何一个的类别并包括它的所有子类别,所以 url 是:

http://localhost:300/api/categories?filter={"where": {"category":  {"exists": false}}, "include": [{"categories": ["categories"]}]}

应该返回的是这个:

[
  {
    _id: 1,
    nome: "Elétrica",
    categories: [
      {
        _id: 2,
        nome: "Tomada",
        categories: [
          {
            _id: 3,
            nome: "Trocar Tomada"
          },
          {
            _id: 4,
            nome: "Tomada em Curto Circuito"
          },
          {
            _id: 5,
            nome: "Outros"
          }
        ]
      }
    ]
  }
]

但它会返回这个:

[
  {
    "nome": "Elétrica",
    "id": "5b7c6e2dcaaa163984a6ee76",
    "categorias": []
  }
]

在我的model.json中,关系是这样设置的:

"relations": {
    "categories": {
      "type": "hasMany",
      "model": "category",
      "foreignKey": "category"
    },
    "category": {
      "type": "belongsTo",
      "model": "category",
      "foreignKey": "category"
    }
  }

要提一提的是,如果我尝试反向查询,那么查询我所有属于其他类别的类别并包含该类别,它可以工作。

也许这不是环回问题或者我的网址是错误的,也许问题是我实际上必须将所有子类别存储在 Mongo 的*类别中,但我不确定,所以如果有人可以如果是这样,请帮助我..

【问题讨论】:

    标签: mongodb relationship loopbackjs


    【解决方案1】:

    我设法通过删除模型中的 belongsTo 关系来开始工作。

    【讨论】: