小编Chr*_*oph的帖子

如何将 JSON 数组转换为 Postgres 数组?

我有一个data类型的列,json其中包含这样的 JSON 文档:

{
    "name": "foo",
    "tags": ["foo", "bar"]
}
Run Code Online (Sandbox Code Playgroud)

我想将嵌套tags数组转换为连接字符串 ( 'foo, bar')。array_to_string()从理论上讲,使用该函数很容易做到这一点。但是,此功能不接受json输入。所以我想知道如何将这个 JSON 数组变成 Postgres 数组(类型text[])?

postgresql array postgresql-9.3 json

97
推荐指数
4
解决办法
28万
查看次数

如何在 postgres 中的整数 json 属性上创建索引

我一生都无法弄清楚如何在我的 json 列的整数属性上创建索引。

我以这种方式尝试过(以及其他数十种方式)

CREATE INDEX user_reputation_idx ON users(("user"->>'reputation')::int)
Run Code Online (Sandbox Code Playgroud)

它在查询中工作得很好(例如ORDER BY ("user"->>'reputation')::int

我错过了什么?

更新

我收到一个简单的语法错误,但是,我真的不知道为什么。

ERROR:  syntax error at or near "::"
LINE 1: ... user_reputation_idx ON users (("user"->>'reputation')::int)
Run Code Online (Sandbox Code Playgroud)

表定义非常简单。它只是一列usertype json

所以,看起来像这样:

CREATE TABLE users
(
  "user" json
)
Run Code Online (Sandbox Code Playgroud)

postgresql index cast postgresql-9.3 json

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

标签 统计

json ×2

postgresql ×2

postgresql-9.3 ×2

array ×1

cast ×1

index ×1