标签: jsonb

如何从 PostgreSQL 的 json 数组中获取具有唯一编号的元素?

我有一张桌子bank_accounts

    Column     |         Type          |                                Modifiers                                | Storage  | Stats target | Description 
---------------+-----------------------+-------------------------------------------------------------------------+----------+--------------+-------------
 id            | integer               | not null default nextval('bank_accounts_id_seq'::regclass)              | plain    |              | 
 name          | character varying(50) |                                                                         | extended |              | 
 bank_accounts | jsonb                 | not null                                                                | extended |              | 
Run Code Online (Sandbox Code Playgroud)

它在jsonb列中有一些 JSON :

 id | name  |                              bank_accounts                               
----+-------+--------------------------------------------------------------------------
  1 | test1 | [{"name": "acct1", "balance": -500}, {"name": "acct2", "balance": -300}]
Run Code Online (Sandbox Code Playgroud)

我正在使用 jsonb_array_elements 获取一个用户的帐户列表:

select jsonb_array_elements(bank_accounts)->>'name' as name, jsonb_array_elements(bank_accounts)->>'balance' as balance …
Run Code Online (Sandbox Code Playgroud)

arrays postgresql json set-returning-functions jsonb

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

Doctrine2 Postgres JSONB 如何搜索

我正在使用 Postgres JSONB,以便在一个复杂系统的一个字段中存储一堆属性。

我有一个字段,称为属性,属性确实有对象树。

这是来自属性的一小部分数据。

{
    "urls": [],
    "games": false,
    "images": [],
    "rating": null,
    "founded": 0,
    "bannedIn": ["00000000-0000-0000-8888-000000000001",
    "00000000-0000-0000-8888-000000000002"],
    "comments": "",
    "chat": false
}
Run Code Online (Sandbox Code Playgroud)

在表中,我有一些对象被禁止在某些 guids 中,一些对象有空数组,而一些对象根本没有bannerIn 属性。

现在我试图获取bannedIn包含的每一行"00000000-0000-0000-8888-000000000001"(WORKS)

当我尝试获取bannedIn不包含的每一行时"00000000-0000-0000-8888-000000000001"(WORKS)

但是当我查询计算所有没有bannedIn的属性时,它会以0计数回退,即使我有1K个没有bannedIn的对象。

作品

$builder = $entityManager->createQueryBuilder()
    ->select('e')
    ->from(Entity::class, 'e')
    ->where('e.otherObject = :guid')->setParameter('guid', $guid)
    ->andWhere('GET_JSON_FIELD(e.properties, \'bannedIn\') LIKE ?1')
    ->setParameter(1, '%00000000-0000-0000-8888-000000000001%');
Run Code Online (Sandbox Code Playgroud)

有没有办法在key不存在的情况下查询jsonb?我也试过跟随,没有奏效。这 ?操作符是通过调用它的函数来实现的jsonb_exists(column_name, value)

php doctrine-orm postgresql-9.3 jsonb

5
推荐指数
0
解决办法
922
查看次数

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

使用 PostgreSQL,如何在 json 列中转义“\”?

我正在使用 Postgres 9.5 并且我有一个类型为“json”的列(“信息”)......我正在尝试执行此插入:

INSERT INTO table ( info )
VALUES (
  '{"entry":"((\+)[0-9]+)"}'
)
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

ERROR:  invalid input syntax for type json
DETAIL:  Escape sequence "\+" is invalid.
Run Code Online (Sandbox Code Playgroud)

它被解释\+为一个转义序列,但我实际上希望它成为我价值的一部分。

postgresql json escaping jsonb

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

在 Postgres 中插入记录时如何合并现有的 jsonb 字段?

假设我在 Postgres 上有一个表,其中 jsonb 列包含{"a": 1, "b": 2}. 现在我想插入一个具有相同 id 和{"b": 10, "c": 20}jsonb 列值的记录。

因此,我希望行的 jsonb 字段包含{"a": 1, "b": 10, "c": 20}. 如何做到这一点?

postgresql jsonb

5
推荐指数
2
解决办法
3375
查看次数

如何通过键值从postgres jsonb中删除元素?

我有一个这种格式的 jsonb 列。

{
    "categoryList": [{
        "category_menu_id": "51",
        "is_featured_product": 0
    }, {
        "category_menu_id": "54",
        "is_featured_product": 1
    }]
}
Run Code Online (Sandbox Code Playgroud)

如何通过 category_menu_id 删除类别?

这个选择查询通过 category_menu_id 工作正常。

select product_category 
from product  
where product_category->'categoryList' @> '[{"category_menu_id": "51"}]';
Run Code Online (Sandbox Code Playgroud)

postgresql jsonb postgresql-9.6

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

Postgres jsonb_set 多个嵌套字段

我有一个带有 jsonb 列的 DB 表,该列有一个实体,带有嵌套的子实体。假设我们有:

SELECT jsonb_set('{"top": {"nested": {"leaf" : 1}}}', '{top,nested,leaf}', '2');

通过更新top.nested.leaf到 2 可以正常工作。

但是如果我们想做多个字段呢,比如:

SELECT jsonb_set('{"top": {"nested": {"leaf" : 1}, "other_nested": {"paper": 0}}}', '[{top,nested,leaf}, {top,other_nested,paper}]', '[2, 2]');

以上不起作用并说:

ERROR: malformed array literal: "[{top,nested,leaf}, {top,other_nested,paper}]" LINE 1: ...": {"leaf" : 1}, "other_nested": {"paper": 0}}}', '[{top,nes... ^ DETAIL: "[" must introduce explicitly-specified array dimensions.

有任何想法吗?

postgresql jsonb

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

从 jsonb_array_elements 或 jsonb_to_recordset 获取 row_number

我正在 jsonb 数组上进行横向交叉连接,我正在寻找数组元素的 row_number (或其等效项)。查看 row_number 文档,我发现除了“分区依据”之外,我还需要执行“排序依据”,但实际上并没有我可以使用的排序标准——数组只有一个设定的顺序,我需要将索引与其余数据一起检索到数组中。

客户端表将有这样的条目

{
  "id": "cj49q33oa000",
  "email": {
    "address": "",
    "after": "2016-06-28T12:28:58.016Z",
    "error": "Et corporis sed."
  },
  "name": "Arnold Schinner",
  "birthdate": "2016-07-29T05:09:33.693Z",
  "status": "paused",
  "sex": "f",
  "waist": [
    {
      "completed": "2017-06-23T10:37:37.500Z"
    },
    {
      "planned": "2017-06-23T10:37:37.500Z"
    },
    {
      "planned": "2017-06-23T10:37:37.500Z"
    },
    {
      "planned": "2017-06-23T10:37:37.500Z"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

我会运行一个查询

SELECT client->>'id' AS id, waist.planned
FROM clients
CROSS JOIN LATERAL JSONB_TO_RECORDSET(client->'waist') AS waist(planned TIMESTAMP WITH TIME ZONE)
WHERE waist.planned IS NOT NULL
Run Code Online (Sandbox Code Playgroud)

但我需要以waist.position_in_array某种方式进入。

postgresql json jsonb

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

从Postgres中的jsonb通过未知键获取元素

我有以下数据结构:

{
    "proccess1": {
        "error": "error1 description",
        "nextRetryAt": "2018-02-22T07:39:00.325Z",
        "attemptsMade": 148,
        "firstFailedAt": "2018-02-16T06:40:41.327Z"
    },
    "proccess2": {
        "error": "error2 description",
        "nextRetryAt": "2019-03-16T06:41:01.566Z",
        "attemptsMade": 77,
        "firstFailedAt": "2016-03-15T04:35:12.248Z"
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是如何构建查询

select * 
from data 
where data->[0]->>'nextRetryAt' = 'my passed value'
Run Code Online (Sandbox Code Playgroud)

我不知道键proccess1proccess2. 他们可能有任何价值。

postgresql json jsonb

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

无法从标量中提取元素

我有 2 表公司和联系人。联系人具有地址 JSONB 列。我在contacts.linked_to_company 上尝试了一个带有join 的select 语句并使用jsonb_array_elements(company.addresses) 但我收到错误“无法从标量中提取元素”,我理解这是因为某些条目在列地址中确实有[null]。我已经看到了使用合并或 CASE 语句的答案。Coalesce 我可能无法工作,CASE 示例在 select 语句中,如何在连接中使用它?这是sql

SELECT company.id,
trading_name, 
nature_of_business, 
t.id contactID, 
address->>'PostCode' Postcode,
position_in_company
FROM contact t FULL JOIN company ON (t.company_linked_to = company.id ),
jsonb_array_elements(t.addresses) address
  WHERE
 t.company_linked_to ='407381';
Run Code Online (Sandbox Code Playgroud)

这是示例 jsonb

[{"PostCode":"BN7788","Address":"South Street","AddressFull":"","Types":[{"Type":"Collection"}]}]
Run Code Online (Sandbox Code Playgroud)

sql postgresql json jsonb

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