首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在typeorm中查找id的数组属性包含特定id的实体

在typeorm中,可以使用Query Builder或Repository来查找id的数组属性包含特定id的实体。

使用Query Builder的方法如下:

  1. 导入typeorm和实体类:
代码语言:txt
复制
import { getRepository } from "typeorm";
import { YourEntity } from "./your-entity";
  1. 创建查询:
代码语言:txt
复制
const entityId = 1; // 特定id
const entities = await getRepository(YourEntity)
  .createQueryBuilder("entity")
  .where(`:entityId = ANY(entity.arrayProperty)`, { entityId })
  .getMany();

这里的YourEntity是你的实体类名,arrayProperty是包含id的数组属性名。:entityId是占位符,用于传递特定id的值。

使用Repository的方法如下:

  1. 导入typeorm和实体类:
代码语言:txt
复制
import { getRepository, Repository } from "typeorm";
import { YourEntity } from "./your-entity";
  1. 创建查询:
代码语言:txt
复制
const entityId = 1; // 特定id
const entityRepository: Repository<YourEntity> = getRepository(YourEntity);
const entities = await entityRepository.createQueryBuilder("entity")
  .where(`:entityId = ANY(entity.arrayProperty)`, { entityId })
  .getMany();

这里的YourEntity是你的实体类名,arrayProperty是包含id的数组属性名。:entityId是占位符,用于传递特定id的值。

以上方法中,getMany()会返回包含特定id的实体数组。

这是typeorm中查找id的数组属性包含特定id的实体的方法。请注意,以上代码示例中的实体类名、属性名和占位符都需要根据你的实际情况进行修改。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

领券