我需要根据文档在我的表中的 JSON 列上添加索引我正在执行类似于下面的查询。
CREATE INDEX idxgin ON api USING gin (jdoc);
我收到以下错误:
错误:数据类型 json 没有用于访问方法“gin”的默认运算符类
提示:您必须为索引指定运算符类或为数据类型定义默认运算符类。
这是文档的链接。
我试图在这里彻底搜索,但没有找到任何答案。
我有一个 PostgreSQL 数据库,它有两个主表:
这两个表有不同的关系。用户可以:
... 一份文件。
问题是我应该如何保存这些关系?
根据我使用 MySQL 的经验,显而易见的方法是为这些多对多关系创建表,包含user_id和document_id.
但是,因为我们使用PostgreSQL,它具有惊人的JSON支持,我们想也许更好的做法是有一个user_document表,其中包含user_id,document_id和JSON列包含所有关系。
JSON 将是这样的:
{
'follow' : {'date' : 1523517140, 'doesFollow' : 't'},
'bookmark' : {'date' : null, 'doesBookmark' : 'f'},
....
}
Run Code Online (Sandbox Code Playgroud)
我对 PostgreSQL 的经验几乎为零,我不知道在 JSONB 列上查询的性能。而且我不知道这种方法在 PostgreSQL 中是否有意义。但它似乎没问题,如果它没有任何问题,也许它比第一种正常方法更可取。
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 表示
我有两个查询,但我只想要一个结果:
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) 在 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)
我有一个具有以下定义的表:
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()函数与#-运算符结合使用(首先从数组中删除值,然后使用更新的状态附加到它)。
我该怎么做?
我有一张桌子:
当我从表中选择所有内容时:
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) 我正在尝试通过 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 …
我正在使用一个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是否有可能通过, 而不是截断来真正获得这种行为?
我刚刚开始使用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) json ×10
postgresql ×6
sql-server ×2
geojson ×1
join ×1
many-to-many ×1
oracle ×1
oracle-21c ×1
postgis ×1
spatial ×1
update ×1