标签: json

使用 GIN 为 JSON 列建立索引

我需要根据文档在我的表中的 JSON 列上添加索引我正在执行类似于下面的查询。

CREATE INDEX idxgin ON api USING gin (jdoc);

我收到以下错误:

错误:数据类型 json 没有用于访问方法“gin”的默认运算符类
提示:您必须为索引指定运算符类或为数据类型定义默认运算符类。

这是文档的链接。

postgresql json postgresql-9.4

2
推荐指数
1
解决办法
1万
查看次数

使用 JSONB 列或另一个表来保存关系

我试图在这里彻底搜索,但没有找到任何答案。

我有一个 PostgreSQL 数据库,它有两个主表:

  • 文件
  • 用户

这两个表有不同的关系。用户可以:

  • 喜欢
  • 书签稍后阅读
  • 节省

... 一份文件。

问题是我应该如何保存这些关系?

根据我使用 MySQL 的经验,显而易见的方法是为这些多对多关系创建表,包含user_iddocument_id.

但是,因为我们使用PostgreSQL,它具有惊人的JSON支持,我们想也许更好的做法是有一个user_document表,其中包含user_iddocument_idJSON列包含所有关系。

JSON 将是这样的:

{
   'follow' : {'date' : 1523517140, 'doesFollow' : 't'}, 
   'bookmark' : {'date' : null, 'doesBookmark' : 'f'},
   ....
}
Run Code Online (Sandbox Code Playgroud)

我对 PostgreSQL 的经验几乎为零,我不知道在 JSONB 列上查询的性能。而且我不知道这种方法在 PostgreSQL 中是否有意义。但它似乎没问题,如果它没有任何问题,也许它比第一种正常方法更可取。

postgresql database-design json many-to-many

2
推荐指数
1
解决办法
3129
查看次数

错误:无效的 GeoJson 表示

GeoJSON 规范展示,

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}
Run Code Online (Sandbox Code Playgroud)

但是当我把它包装在一个调用中时ST_GeomFromGeoJSON,像这样,

SELECT ST_GeomFromGeoJSON($${                                                                                                                          
  "type": "Feature",                                                                                                                                   
  "geometry": {                                                                                                                                        
    "type": "Point",                                                                                                                                   
    "coordinates": [125.6, 10.1]                                                                                                                       
  },                                                                                                                                                   
  "properties": {                                                                                                                                      
    "name": "Dinagat Islands"                                                                                                                          
  }                                                                                                                                                    
}$$);
Run Code Online (Sandbox Code Playgroud)

我得到错误,

错误:无效的 GeoJson 表示

postgresql spatial postgis json geojson

2
推荐指数
1
解决办法
1685
查看次数

两个查询,一个 JSON 结果

我有两个查询,但我只想要一个结果:

1 查询,给我列的所有数据

 SELECT * FROM dbo.Notificacion
 FOR JSON PATH, ROOT('notification')
Run Code Online (Sandbox Code Playgroud)

2 查询给我所有的列名

 SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'notificacion' ORDER BY ORDINAL_POSITION
 FOR JSON PATH, ROOT('COLUMN_NAME')
Run Code Online (Sandbox Code Playgroud)

这样,我可以使应用程序的视图更加动态。

我想要这样的 JSON:

 SELECT * FROM dbo.Notificacion
 FOR JSON PATH, ROOT('notification')
Run Code Online (Sandbox Code Playgroud)

sql-server json

2
推荐指数
1
解决办法
49
查看次数

以 JSON 文本形式返回结果集 — 使用漂亮的格式

在 Oracle 21c 中,看起来我们可以以 JSON 文本形式返回结果集:

SELECT json_object(*)
from dual;

result:
{"DUMMY":"X"}
Run Code Online (Sandbox Code Playgroud)

这适用于db<>fiddle

在此输入图像描述

问题:

有没有办法使用漂亮的格式返回 JSON 文本?

{
   "DUMMY":"X"
}
Run Code Online (Sandbox Code Playgroud)

相关:使用 SQL 选择 SDO_GEOMETRY 的 JSON 文本

oracle json oracle-21c

2
推荐指数
1
解决办法
748
查看次数

如何更新复杂的 jsonb 列?

我有一个具有以下定义的表:

create table json_test (
    filter_data jsonb);
Run Code Online (Sandbox Code Playgroud)

我插入这样的值:

'{"task_packets": [
    {
        "state": "PROCEEDING",
        "task_id": 1001
    },
    {
        "state": "REVERTING",
        "task_id": 1002
    }
]}'
Run Code Online (Sandbox Code Playgroud)

现在我想将此jsonb列更新为:

'{"task_packets": [
    {
        "state": "DONE",
        "task_id": 1001
    },
    {
        "state": "REVERTING",
        "task_id": 1002
    }
]}'
Run Code Online (Sandbox Code Playgroud)

即我想使用指定的task_id内部 task_packets 数组更改值的状态。我建议以某种方式将jsonb_set()函数与#-运算符结合使用(首先从数组中删除值,然后使用更新的状态附加到它)。
我该怎么做?

postgresql update json postgresql-9.5

1
推荐指数
1
解决办法
2万
查看次数

json 字段上的“where”导致算术溢出错误

我有一张桌子:

当我从表中选择所有内容时:

 SELECT JSON_VALUE(LastReadings, '$.lastReadings."7-temp".value') as x FROM
(VALUES    
(2,N'{"groups": [],"lastReadings": {"amir": {"name": "Ink","value":12.0},"7-temp": {"name": "Temperature","value":12}},"customId":null}'),
(3,N'{"groups": [],"lastReadings": {"amir": {"name": "Ink","value":12.0},"7-temp": {"name": "Temperature","value":32}},"customId":null}'),
(4,N'{"groups": [],"lastReadings": {"amir": {"name": "Ink","value":12.0},"7-temp": {"name": "Temperature","value":22}},"customId": null}'),
(5,N'{"groups": [],"lastReadings": {"amir": {"name": "Ink","value": 12.0},"7-temp": {"name": "Temperature","value":123}},"customId": null}')    
)
[AmirTestTable](Id,LastReadings )
Run Code Online (Sandbox Code Playgroud)

我得到

+-----+
|  x  |
+-----+
|  12 |
|  32 |
|  22 |
| 123 |
+-----+
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试添加 where 子句时:

 SELECT JSON_VALUE(LastReadings, '$.lastReadings."7-temp".value') as x 
 FROM
(VALUES
  (2,N'{"groups": [],"lastReadings": {"amir": {"name": "Ink","value":12.0},"7-temp": {"name": "Temperature","value":12}},"customId":null}'),
  (3,N'{"groups": …
Run Code Online (Sandbox Code Playgroud)

json sql-server-2016

1
推荐指数
1
解决办法
331
查看次数

将 JSONB 列连接到 Postgres 中的普通 PK 列

我正在尝试通过 Postgres 中 jsonb 字段上的属性将普通表连接到具有 jsonb 字段的表。例子:

普通表:

create table Res
(
    id uuid default uuid_generate_v4() not null
        constraint res_pkey
            primary key
)
;
Run Code Online (Sandbox Code Playgroud)

带有 jsonb 的表:

create table res_rem_sent
(
    body jsonb not null,
    search tsvector,
    created_at timestamp with time zone default now(),
    id uuid default uuid_generate_v4() not null
        constraint res_rem_sent_pkey
            primary key
);

create index idx_res_rem_sent
    on res_rem_sent (body)
;

create index idx_search_res_rem_sent
    on res_rem_sent (search)
;
Run Code Online (Sandbox Code Playgroud)

body字段可能如下所示:{"resId": <GUID>, ....}

我想将res表连接到id等于给定 GUID id …

postgresql join json postgresql-9.6

1
推荐指数
1
解决办法
6839
查看次数

jsonb 和数字精度和小数位数

我正在使用一个jsonb列,其中 JSON 文档包含大整数 (PostgreSQL 9.5)。我注意到,当存储的值有太多有效数字时,它会被截断。

作为示例,我将其插入到我的表中:

{"value": 7598786232076607106}
Run Code Online (Sandbox Code Playgroud)

当我选择返回同一行时,我得到:

{"value": 7598786232076607000}
Run Code Online (Sandbox Code Playgroud)

根据文档numeric,jsonb 中的数字字段使用postgres 的标准类型存储,并且numeric通常允许:

小数点前最多 131072 位;小数点后最多 16383 位

jsonb是否有可能通过, 而不是截断来真正获得这种行为?

postgresql json postgresql-9.4

1
推荐指数
1
解决办法
1711
查看次数

使用 OPENJSON 生成嵌套数组非常慢

我刚刚开始使用OPENJSONSQL Server 2016 SP1。

我有这句话:

select c.Serial as Parent,
    (Select co.Serial, agc.Position
      from AggregationChildren agc, Aggregation ag, Code co
      where agc.AggregationId = a.AggregationId 
      and co.CodeId = agc.AggregationChildrenId for json path) as children
    from Aggregation a, Code c
    where c.CodeId = a.AggregationId for json path
Run Code Online (Sandbox Code Playgroud)

要生成此 JSON:

select c.Serial as Parent,
    (Select co.Serial, agc.Position
      from AggregationChildren agc, Aggregation ag, Code co
      where agc.AggregationId = a.AggregationId 
      and co.CodeId = agc.AggregationChildrenId for json path) as children
    from Aggregation a, Code c …
Run Code Online (Sandbox Code Playgroud)

sql-server json

1
推荐指数
1
解决办法
1229
查看次数