【问题标题】:How can I findAll records which has ID in array of objects?如何在对象数组中找到所有具有 ID 的记录?
【发布时间】:2020-07-22 03:52:20
【问题描述】:

如果我使用的是 Op.eq:

   where: {
        id: {
          [op.eq]: 5,
        },
      },

我正在获取 ID-5 用户的聊天列表。但我在用户字段中看不到其他聊天参与者

…
    “name”: “x”
    "users": [
      {
        "id": 5,
      }
    ]
  }
…

如果我使用不带运算符的 findAll,我会得到:

…
    “name”: “x”
    "users": [
      {
        "id": 5,
      },
      {
        "id": 2,
      }
    ]
  }
…

那么,我如何获取对象数组中有 ID 的记录列表

当我使用 [Op.in] 运算符时,我失去了数组中的其他参与者

【问题讨论】:

  • 请分享更多代码并在输出中描述您想要的内容。

标签: javascript orm graphql sequelize.js


【解决方案1】:

您可以使用 or 运算符。

YourModel.findAll({
  where: {
    [Op.or]: [
        {id: 5},
        {id: 2}
    ] // You can add all your array items here in this format dynamically.
  }
});

【讨论】:

    猜你喜欢
    • 2018-12-11
    • 2014-06-02
    • 2023-03-20
    • 1970-01-01
    • 2016-09-21
    • 2017-03-01
    • 2016-10-18
    • 2019-05-03
    • 1970-01-01
    相关资源
    最近更新 更多