如何搜索过的Postgres的JSON类型的多级系统使用活动记录 [英] how do I search over a json type of postgres in multilevels using active record

查看:191
本文介绍了如何搜索过的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类型的多级系统使用活动记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆