String Commands
5 methods对 Redis 字符串值进行操作的命令。
jsonb_set(target jsonb, path text[], new_value jsonb, create_missing bool = true) -> jsonb将 key 设为 value,可选过期和存在条件标志。
Parameters
| Name | Type | Description |
|---|---|---|
| target | jsonb | Source JSON document. |
| path | text[] | Array of keys/indices to the target field. |
| new_value | jsonb | Replacement value. |
| create_missing | bool | If true, creates missing keys. |
Returns
jsonb
Example
postgresql
UPDATE profile
SET data = jsonb_set(data, '{address,city}', '"Berlin"', true)
WHERE id = 1;jsonb_build_object(key text, value any, ...) -> jsonb返回 key 存储的字符串值,如果 key 不存在则返回 nil。
Parameters
| Name | Type | Description |
|---|---|---|
| key | text | Object key. |
| value | any | Value converted to JSON. |
Returns
jsonb
Example
postgresql
SELECT jsonb_build_object('id', id, 'name', name, 'admin', is_admin) FROM users;jsonb @> jsonb -> bool将 key 存储的整数值原子递增一或给定增量。
Parameters
| Name | Type | Description |
|---|---|---|
| left | jsonb | Container document. |
| right | jsonb | Contained document. |
Returns
bool
Example
postgresql
SELECT * FROM items WHERE tags @> '["sale"]';jsonb ? text -> bool将 value 追加到 key 的字符串,如不存在则创建。返回新长度。
Parameters
| Name | Type | Description |
|---|---|---|
| key | text | Key or array element to test. |
Returns
bool
Example
postgresql
SELECT * FROM config WHERE data ? 'feature_x';jsonb_agg(expr ORDER BY ...) -> jsonb在 key 上设置以秒为单位的生存时间,可选条件标志。
Parameters
| Name | Type | Description |
|---|---|---|
| expr | any | Expression aggregated per group. |
Returns
jsonb
Example
postgresql
SELECT user_id, jsonb_agg(id ORDER BY created_at) AS order_ids
FROM orders GROUP BY user_id;