栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Postgres JSON数据类型Rails查询

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Postgres JSON数据类型Rails查询

对于任何偶然发现的人。我想出了一个使用ActiveRecord和Postgres的JSON数据类型的查询列表。随时对其进行编辑以使其更加清晰。

以下使用的JSON运算符的文档:https : //www.postgresql.org/docs/current/functions-
json.html

# Sort based on the Hstore data:Post.order("data->'hello' DESC")=> #<ActiveRecord::Relation [    #<Post id: 4, data: {"hi"=>"23", "hello"=>"22"}>,     #<Post id: 3, data: {"hi"=>"13", "hello"=>"21"}>,     #<Post id: 2, data: {"hi"=>"3", "hello"=>"2"}>,     #<Post id: 1, data: {"hi"=>"2", "hello"=>"1"}>]># Where inside a JSON object:Record.where("data ->> 'likelihood' = '0.89'")# Example json object:r.column_data=> {"data1"=>[1, 2, 3],     "data2"=>"data2-3",     "array"=>[{"hello"=>1}, {"hi"=>2}],     "nest"=>{"nest1"=>"yes"}}# Nested search:Record.where("column_data -> 'nest' ->> 'nest1' = 'yes' ")# Search within array:Record.where("column_data #>> '{data1,1}' = '2' ")# Search within a value that's an array:Record.where("column_data #> '{array,0}' ->> 'hello' = '1' ")# this only find for one element of the array.# All elements:Record.where("column_data ->> 'array' LIKE '%hello%' ") # badRecord.where("column_data ->> 'array' LIKE ?", "%hello%") # good


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616679.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号