如何使用Typescript/Javascript在KNEX中过滤基于JSONB列的数据库数据



请帮我指明正确的方向:我的一个表中有一个jsonb列。它保存字符串数据如下:

entry: {
1: "data1",
2: "data2"
}

如果我想获取键1为data1的条目,我该如何编写查询?下面抛出一个sql错误:

sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '['1'] = 'data1' limit 500' at line 1",

以下是我在模型中使用的内容:

return await db(db_table).select('*')
.limit(per_page).where('form_id', form_id)
.whereRaw("entry->>[?] = ?",['1', 'data1']); // issue is here

如何构造最后一行来搜索json字段/列,并返回表中键1等于data1的所有过滤行?非常感谢。

它应该是这样的:

return await db(db_table).select('*')
.limit(per_page).where('form_id', form_id)
.whereRaw("entry->>$.?? = ?",['1', 'data1']); // issue is here
// ------------------^ this extra ? marks a placeholder for a column

最新更新