我正在为演示应用程序使用nest js framework
。在我的演示应用程序中,我有一个entity
名称:Category
,其中有两个列(id,描述)。描述是jsonb类型
export class Category extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'jsonb' })
description;
}
我在表中插入了一些数据。
的行当前使用下面的代码给我所有行。但是我只需要具有name equals to test
的行。当前只有一行
@Get('/app')
async getApp(): Promise<Category[]> {
return this.categoryRepository.find({});
}
这是我的代码https://codesandbox.io/s/nest-nhqyb?file=/src/app.controller.ts:568-670
您可以看到类似的过滤结果
根据this issue,除非您正在编写原始SQL并使用数据库特定的语法,否则是不可能的。
在Postgres中,看起来像
SELECT *
FROM Category cat
WHERE EXISTS (
SELECT FROM jsonb_each(cat.description) desc
WHERE desc.value->>'name' ILIKE 'test'
);
进行更多讨论,并有深入的答案over here