Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套JSONB字段中对象的Postgresql查询_Json_Postgresql_Jsonb_Postgresql 9.6_Postgresql 12 - Fatal编程技术网

嵌套JSONB字段中对象的Postgresql查询

嵌套JSONB字段中对象的Postgresql查询,json,postgresql,jsonb,postgresql-9.6,postgresql-12,Json,Postgresql,Jsonb,Postgresql 9.6,Postgresql 12,我使用的是PostgreSQL 9.6,我有一个名为“ItemDbModel”的表,有两列,如下所示: No integer, Content jsonb 假设我放了很多唱片,比如: "No": 2, {"obj":"x","Item": {"Name": "BigDog", "Model": "NamedHusky", "Spec":"red dog"}} "No": 4, {"obj":"x","Item": {"Name": "MidDog", "Model": "NamedPepp

我使用的是PostgreSQL 9.6,我有一个名为“ItemDbModel”的表,有两列,如下所示:

No integer,
Content jsonb
假设我放了很多唱片,比如:

 "No": 2, {"obj":"x","Item": {"Name": "BigDog", "Model": "NamedHusky", "Spec":"red dog"}}
 "No": 4, {"obj":"x","Item": {"Name": "MidDog", "Model": "NamedPeppy", "Spec":"no hair"}}
 "No": 5, {"obj":"x","Item": {"Name": "BigCat", "Model": "TomCat", "Spec":"blue color"}}
如何查询表中的以下内容:

  • “Content.Item.Name”包含“Dog”且“Content.Item.Spec”包含“red”的记录
  • “Content.Item.Name”包含“Dog”或“Content.Item.Spec”包含“red”的记录
  • “Content.Item”中的任何json字段包含“dog”的记录
  • 并按“内容、项目、名称、长度”排序


    谢谢大家!

    你应该熟悉

    Postgres 12引入了实现SQL/JSON路径语言的新功能。使用
    jsonpath
    的替代查询可能如下所示:

    -- #1
    select *
    from example
    where jsonb_path_exists(
        content, 
        '$ ? ($.Item.Name like_regex "dog" flag "i" && $.Item.Spec like_regex "red" flag "i")');
    
    -- #2
    select *
    from example
    where jsonb_path_exists(
        content, 
        '$ ? ($.Item.Name like_regex "dog" flag "i" || $.Item.Spec like_regex "red" flag "i")');
    
    -- #3
    select *
    from example
    where jsonb_path_exists(
        content, 
        '$.Item.* ? (@ like_regex "dog" flag "i")');
    
    前两个查询基本上与前面的查询相似,
    ->
    语法似乎比
    jsonpath
    更简单、更令人愉快。应特别注意第三个查询,它使用通配符,因此无需使用昂贵的函数
    jsonb_each_text()
    ,并且速度应该更快

    阅读文档:


    非常感谢!是的,我现在正在读给定的文章!我可以知道我们如何在“->”和“->>”之间进行选择吗?
    ->
    操作符给出一个json对象,而
    ->
    生成文本。老实说,对Postgres感到失望。这些运算符不必要地混淆了。@jstuartmilne-
    i
    表示不区分大小写的匹配,有关详细信息,请参阅。
    -- #1
    select *
    from example
    where jsonb_path_exists(
        content, 
        '$ ? ($.Item.Name like_regex "dog" flag "i" && $.Item.Spec like_regex "red" flag "i")');
    
    -- #2
    select *
    from example
    where jsonb_path_exists(
        content, 
        '$ ? ($.Item.Name like_regex "dog" flag "i" || $.Item.Spec like_regex "red" flag "i")');
    
    -- #3
    select *
    from example
    where jsonb_path_exists(
        content, 
        '$.Item.* ? (@ like_regex "dog" flag "i")');