ruby-on-rails - 查询 Rails/Arel/Postgresql 中存在 json 键的记录
问题描述
在带有名为 的列的假设books
表上,我想召唤有和(分别)没有键的记录。json
internal
rating
解决方案
存在:
Book.where('internal::jsonb ? :key', key: 'rating')
不存在:
Book.where.not('internal::jsonb ? :key', key: 'rating')
jsonb ? text → boolean
Does the text string exist as a top-level key or array element within the JSON value?
'{"a":1, "b":2}'::jsonb ? 'b' → t
'["a", "b", "c"]'::jsonb ? 'b' → t
推荐阅读
- mongodb - MongoDB $push 聚合不会保持正确的顺序
- python - 结合来自两个数据帧的热编码
- windows - 如何使用“nsis-web”包创建科尔多瓦电子构建?
- c# - .NET core Semaphore 和 SemaphoreSlim 在 linux 服务器上工作
- ansible - 从每个库存主机的多个项目中循环注册变量
- c# - .Net Core 中的异步委托
- c# - 玩家在 Unity 3D 中穿过墙壁
- sql - MS SQL - 加入多个枢轴
- javascript - 如何访问从烧瓶服务器传递到 jinja 模板中的 js 函数的数据帧值?
- c - 共享 C 静态库 - 包含/排除什么