【问题标题】:How do you find an item in a repository by something other than id您如何通过 id 以外的其他方式在存储库中找到项目
【发布时间】:2019-06-28 21:14:50
【问题描述】:

如果我有一个包含许多属性的存储库,并且我想通过非 id 属性查找某些内容,我是否只需查找所有内容,然后在布尔比较后返回数据,还是有更好的方法来查找属性那不是身份证?

【问题讨论】:

    标签: loopbackjs strongloop


    【解决方案1】:

    在 loopback4 中,您需要为此目的使用存储库。执行以下操作。

    如果您知道只有一个具有价值的条目。 (唯一列)

    const user = await this.userRepository.findOne({
      where: {
        username: 'test_admin'
      }
    });
    

    对于可以有多个的情况。

    const user = await this.userRepository.find({
      where: {
        firstName: 'test admin'
      }
    });
    

    【讨论】:

    • 如果这解决了您的问题,如果您能接受这个答案,我会很高兴。
    • 是否可以在哪里进行比较?像 where count (column) > 1?
    • 可以。其中:{ 计数:{ gt: 1 } }
    【解决方案2】:

    对于 Loopback 3,您可以在此处找到查询数据的文档:https://loopback.io/doc/en/lb3/Querying-data.html

    基本上,使用这样的查询过滤器:

    const objects = await app.models.ModelName.find(
      {
        where: {
          propertyName: value
        }
      }
    )

    不要忘记为要查询的属性定义索引,否则数据库引擎将执行全表扫描。

    "properties": {
      "propertyName": {
        "type": "string",
        "index": {
          "unique": true
        }
      },
      ...
    }

    【讨论】:

    • 环回 4 怎么样?
    • 没用过,请在题名中注明Loopback 4
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2018-07-09
    相关资源
    最近更新 更多