Node.js 使用sequelize查询Postgres嵌套的JSONB列

Node.js 使用sequelize查询Postgres嵌套的JSONB列,node.js,sequelize.js,sequelize-cli,Node.js,Sequelize.js,Sequelize Cli,您好,我有一个表,在其中我使用JSONB存储嵌套的JSON数据,并且需要查询这个JSONB列 以下是表格的结构 { "id": "5810f6b3-fefb-4eb1-befc-7df11a24d997", "entity": "LocationTypes", "event_name": "LocationTypes adde

您好,我有一个表,在其中我使用JSONB存储嵌套的JSON数据,并且需要查询这个JSONB列 以下是表格的结构

 {
          "id": "5810f6b3-fefb-4eb1-befc-7df11a24d997",
          "entity": "LocationTypes",
          "event_name": "LocationTypes added",
          "data": {
            "event":{
            "id": "b2805163-78f0-4384-bad6-1df8d35b456d",
            "name": "builidng",
            "company_id": "1dd83f77-fdf1-496d-9e0b-f502788c3a7b",
            "is_address_applicable": true,
            "is_location_map_applicable": true}
          },
          "notes": null,
          "event_time": "2020-11-05T10:56:34.909Z",
          "company_id": "1dd83f77-fdf1-496d-9e0b-f502788c3a7b",
          "created_at": "2020-11-05T10:56:34.909Z",
          "updated_at": "2020-11-05T10:56:34.909Z"
        }
下面的代码给出了空白数组作为响应


    const dataJson = await database.activity_logs.findAll({
          where: {
            'data.event.id': {
              $eq: 'b2805163-78f0-4384-bad6-1df8d35b456d',
            },
          },
          raw: true,
        });


有什么方法可以更好地使用sequelize查询嵌套的json对象。

您应该尝试
sequelize.literal
,将json运算符/函数包装到
sequelize.where
。大概是这样的:

sequelize.where(sequelize.literal("data->'event'->'id'"), '=', 'b2805163-78f0-4384-bad6-1df8d35b456d')

感谢@Anatoly的回复,很抱歉,我在数据列中还有一个级别是data.event.id而不是data.id。我已经编辑了这个问题,这是我应该如何使用的吗?const dataJson=wait database.activity_logs.findAll(sequelize.where(sequelize.literal(“数据->'new'>'id',=','c44234ba-79a7-4d94-a378-7568dd4a8f5c'));几乎。不要忘记在
where
选项中指明
sequelize.where
findAll({where:sequelize.where(sequelize.literal(“数据->'new'>'id',=','c4434ba-79a7-4d94-a378-7568dd4a8f5c'))当我使用上面的代码选择“id”,“entity”,“event\u name”,“data”,“notes”,“event\u time”,“实体id”、“用户id”、“客户id”、“请求id”、“源ip”、“公司id”、“创建位置”、“更新位置”从“活动日志”改为“活动日志”,其中数据->'new'>'id'为空;My bad.应为
sequelize.WHERE(sequelize.literal(“数据->'new'>'id'),'c44234ba-79a7-4d94-a378-7568dd4a8f5c')
。我更新了答案