node.js - 获取 postgres db 的 jsonb 列
问题描述
我node-postgres
用来选择和插入数据到postgres
. 我有一些jsonb
类型的列,我通过使用以下查询从 db 获取
getEmployee() {
return SELECT empId, empData FROM employee WHERE empId = $1;
}
empData
列的类型在哪里jsonb
。下面是使用上述查询的代码片段。
const employee = await DBService.query(pgObj.getEmployee(), [empId]);
当我试图从中得到时empData
,employee
我得到的是空值。
const { empData } = employee;
我不确定我在这里缺少什么。这是获取dbjosnb
列的正确方法吗?postgreas
nodejs
解决方案
您确定empdata
甚至在数据库中填充了吗?也许它是空的。另外,什么是 jsonb 字段empdata
?
要获取 的实际子字段empdata
,您需要->>
运算符。例如:
将整个 json 对象作为文本获取
SELECT empId, empData::text
FROM employee where empId = $1
获取单个属性
SELECT empId, empData->>annual_pay as salary
FROM employee WHERE empId = $1;
等等......你也可以试试看这里:https ://kb.objectrocket.com/postgresql/how-to-query-a-postgres-jsonb-column-1433
我还没有尝试过这些,我现在不在 postgres 面前。
推荐阅读
- python - 具有动态滞后的窗口函数
- jenkins - Jenkins 不会运行我的 NUnit 测试,但 cmd 会
- java - JNI,我找不到内存泄漏的代码位置
- c# - C# RDLC 报告 - 直接到打印机 - 格式折叠
- ruby-on-rails - Rails:需要帮助了解连接表、嵌套表单、关联
- python - 在脚本执行期间询问时如何从python脚本或shell脚本输入密码
- c# - 使用 PlaybackStopped 事件时,Naudio 崩溃问题
- qt - Qml - 为什么删除模型时不会删除tableview中delegate的数据
- node.js - 在nodejs自定义模块中获取res对象
- objective-c - cmake:在 macOS 上找不到命令