Skip to content

Redis JSONB API

Redis 字符串命令,用于存储、读取和原子更新单个值。

1 class · 5 methods

String Commands

5 methods

对 Redis 字符串值进行操作的命令。

jsonb_set(target jsonb, path text[], new_value jsonb, create_missing bool = true) -> jsonb

将 key 设为 value,可选过期和存在条件标志。

Parameters

NameTypeDescription
targetjsonbSource JSON document.
pathtext[]Array of keys/indices to the target field.
new_valuejsonbReplacement value.
create_missingboolIf 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

NameTypeDescription
keytextObject key.
valueanyValue 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

NameTypeDescription
leftjsonbContainer document.
rightjsonbContained document.

Returns

bool

Example

postgresql
SELECT * FROM items WHERE tags @> '["sale"]';
jsonb ? text -> bool

将 value 追加到 key 的字符串,如不存在则创建。返回新长度。

Parameters

NameTypeDescription
keytextKey or array element to test.

Returns

bool

Example

postgresql
SELECT * FROM config WHERE data ? 'feature_x';
jsonb_agg(expr ORDER BY ...) -> jsonb

在 key 上设置以秒为单位的生存时间,可选条件标志。

Parameters

NameTypeDescription
expranyExpression 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;