You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeORM 0.3.17:如何用find()实现AND嵌套OR的WHERE查询(不使用QueryBuilder)

可以用find()方法实现该查询

在TypeORM 0.3.17中,无需使用QueryBuilder,仅通过find()方法的where选项就能实现AND(OR(value1, value2), value3)的查询结构。核心是利用where选项的数组嵌套规则:

  • 外层数组的元素之间默认是AND关系
  • 内层数组的元素之间默认是OR关系

具体实现代码如下:

const photoRepository = AppDataSource.getRepository(Photo);
const photos = await photoRepository.find({
  where: [
    // OR条件:name匹配query 或 location匹配query
    [{ name: query }, { location: query }],
    // AND的第二个条件:status为Active
    { status: Status.Active }
  ]
});

这段代码会生成等价于以下SQL逻辑的查询:

WHERE (name = ? OR location = ?) AND status = ?

内容的提问来源于stack exchange,提问作者Enzo Rodrigues

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 01:57:09