Postgresql 类型化jsonb数组查询
我试图查询一个jsonb数组 列中的示例数据Postgresql 类型化jsonb数组查询,postgresql,nestjs,jsonb,typeorm,Postgresql,Nestjs,Jsonb,Typeorm,我试图查询一个jsonb数组 列中的示例数据 [{"entityId": "1ebi", "entityType": "user", "mode": "editor"}, {"entityId": "F13t", "entityType": "team", "mode": &qu
[{"entityId": "1ebi", "entityType": "user", "mode": "editor"}, {"entityId": "F13t", "entityType": "team", "mode": "viewer"}]
实体定义
@Column({ name: 'details', type: 'jsonb' })
public details: {entityId: string, entityType: string, mode: string}[];
这个查询似乎不起作用。我看到这个后就试过了。但我的数据是一个JSON数组
this.workbookPermissionRepository
.createQueryBuilder('wp')
.select()
.where('wp.details ::jsonb @> :details', {
details: {
entityType: IPermissionEntityTypes.USER,
entityId: user.slug,
mode: Not(IPermissionSharingModes.NO_ACCESS),
},
})
.printSql()
.getMany();
我没有在打字机上找到任何东西。欢迎任何建议。在json数组上查询Postgres时的语法要求将查询的右侧用括号括起来。在这种情况下,请尝试将查询包装在括号中,并按如下方式对其进行字符串化:
this.workbookPermissionRepository
.createQueryBuilder('wp')
.select()
.where('wp.details ::jsonb @> :details', {
details: JSON.stringify([{
entityType: IPermissionEntityTypes.USER,
entityId: user.slug,
mode: Not(IPermissionSharingModes.NO_ACCESS),
}])
})
.printSql()
.getMany();
下面是一篇类似的文章,可以帮助解释基本的Postgres查询: