【问题标题】:How to filter Subscribers based on array of tags in Loopabck如何根据 Loopabck 中的标签数组过滤订阅者
【发布时间】:2019-03-09 11:00:14
【问题描述】:

我有两个模型 - 订阅者标签

样本数据:

{
 subscribers: [
  {
   name: "User 1",
   tags: ["a","b"] 
  },
  {
   name: "User 2",
   tags: ["c","d"] 
  }
 ]
}

我想根据标签过滤订阅者。

  • 如果我给ab标签,用户1应该列出
  • 如果我给ac标签, 用户 1 和用户 2 都应该列出

这是我尝试过的:

方法一:

tagssubscribers 模型中的列,具有数组数据类型

/subscribers/?filter={"where":{"tags":{"inq":["a","b"]}}} // doesn't work

方法二:

创建了一个单独的表tags 并设置subscribers 有很多tags

/subscribers/?filter={"where":{"tags":{"inq":["a","b"]}}} // doesn't work

如何在 Loopback 中实现这一点而不编写自定义方法?

我有 Postgresql 作为连接器

【问题讨论】:

    标签: node.js postgresql loopbackjs loopback


    【解决方案1】:

    终于找到了一个技巧,使用正则表达式!这不是一个高性能的解决方案,但它有效!

    { "where": { "tags": { "regexp": "a|b" } } }

    【讨论】:

      【解决方案2】:

      更新

      正如loopback docs 中提到的,您应该使用inq 而不是In

      inq 运算符检查指定属性的值是否与数组中提供的任何值匹配。一般语法是: {where: { property: { inq: [val1, val2, ...]}}}

      从这里:

      /subscribers/?filter={"where":{"tags":{"In":["a","b"]}}}

      到这里:

      /subscribers/?filter={"where":{"tags":{"inq":["a","b"]}}}

      【讨论】:

      • 这个问题是 Loopback 特有的
      • @GijoVarghese 我误解了您的要求并相应地更新了我的答案
      • 那行不通。为了使其工作,tags 列不应该是数组
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 2019-02-17
      • 2023-02-05
      • 2017-10-04
      相关资源
      最近更新 更多