如何将 table 中每一行的 JSONB 字符串数组的所有元素小写

How to lower-case all the elements of a JSONB array of strings of each row in a table

我有一个 table,其中有一个名为 "data" 的字段,它是 JSONB 类型的。 "data" 的内容是一个对象,其中包含一个名为 "associated_emails" 的字段,它是一个字符串数组。

我需要更新现有的table,使"associated_emails"的内容全部小写。如何实现?这是我到目前为止的尝试(它触发错误:ERROR: cannot extract elements from a scalar

update mytable my
set
    "data" = safe_jsonb_set(
        my."data",
        '{associated_emails}',
        to_jsonb(
            lower(
                (
                    SELECT array_agg(x) FROM jsonb_array_elements_text(
                        coalesce(
                            my."data"->'associated_emails',
                            '{}'::jsonb
                        )
                    ) t(x)
                )::text[]::text
            )::text[]
        )
    )
where
  my.mytype = 'something';

您想使用 JSONB_SET 并使用如下所示的内容更新该列:

UPDATE jsonb_test 
SET    data = JSONB_SET(data, '{0,associated_emails}', 
                        JSONB(LOWER(data ->> 'associated_emails'::TEXT)));