如何搜索过的Postgres的JSON类型的多级系统使用活动记录

编程入门 行业动态 更新时间:2024-10-27 02:19:09
本文介绍了如何搜索过的Postgres的JSON类型的多级系统使用活动记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

supose我下面

的JSON

:response_body => {        状态=> 好,            嗯=> {            一个=> 一个,                的b=> {                    C=> 好            }    }

。例我的搜索:

LogService.where(response_body:{状态=>中确定})

解决方案

如果您正在使用的Postgres数据库和列确实是一个JSON类型,您可以使用Rails中这样的搜索:

LogService.where(response_body#>>{}状态'='OK')

这#>>将走过去的JSON路径并进行搜索。 如果你只使用#>(只需>),它将返回remaing JSON。

另外一个例子,如果你想获得C=>确定

LogService.where(response_body#>>'{嗯,B,C}'='OK')

我觉得that's吧。

supose I have the json below

:response_body => { "status" => "ok", "um" => { "a" => "a", "b" => { "c" => "ok" } }

Exemple my search:

LogService.where(response_body: {"status" => "ok"})

解决方案

If you are using the Postgres as database and the column really is a json type, you can use a search in Rails like this:

LogService.where("response_body#>>'{status}' = 'ok'")

This #>> will walk over the json path and make the search. If you use only #> (with just one >), it will return the remaing json.

Another example, if you want to get the "c" => "ok"

LogService.where("response_body#>>'{um, b, c}' = 'ok'")

I think that´s it.

更多推荐

如何搜索过的Postgres的JSON类型的多级系统使用活动记录

本文发布于:2023-10-16 10:43:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497342.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   系统   Postgres   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!