简体   繁体   English

Rails - Postgres - jsonb 列查询

[英]Rails - Postgres - jsonb column query

I have a jsonb column named 'available_quantity'.我有一个名为“available_quantity”的 jsonb 列。 which will have sample values这将有样本值

{ "100": 50, "1000":10 }

Now, I want to query all keys with values less than 50.现在,我想查询所有值小于 50 的键。

I tried this query,我试过这个查询,

Bundle.where('available_quantity @> ?', {'100': 5}.to_json)

But this one gives me all the Bundle with available_quantity containing {100: 5}.但是这个给了我所有包含 {100:5} 的可用数量的捆绑包。

How can I do that?我怎样才能做到这一点? Is that even possible?这甚至可能吗?

You can use the ->> operator:您可以使用->>运算符:

Bundle.where("(available_quantity->>'100')::int < 50")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM