contains()
该方法仅用于在 jsonb、数组(array)和范围(range)列上进行过滤
contains()的作用是匹配包含指定元素的行。
换句话说,在指定列中,匹配出来的记录的值是给定数组(array)
的子集。
也就是说,在指定列中,给定的数组(array)
包含了匹配出记录的所有元素。
案例1 (关于数组列) link
create table
issues (
id int8 primary key,
title text,
tags text[]
);
insert into
issues (id, title, tags)
values
(1, 'Cache invalidation is not working', array['is:open', 'severity:high', 'priority:low']),
(2, 'Use better names', array['is:open', 'severity:low', 'priority:medium']);
const { data, error } = await supabase
.from('users')
.select()
.contains('tags', ['is:open', 'severity:high', 'priority:low']);
{
"data": [
{
"title": "Cache invalidation is not working"
}
],
"status": 200,
"statusText": "OK"
}
案例2 (关于范围列) link
create table
reservations (
id int8 primary key,
room_name text,
during tsrange
);
insert into
reservations (id, room_name, during)
values
(1, 'Emerald', '[2000-01-01 13:00, 2000-01-01 15:00)'),
(2, 'Topaz', '[2000-01-02 09:00, 2000-01-02 10:00)');
const { data, error } = await supabase
.from('reservations')
.select()
.contains('during', '[2000-01-01 13:00, 2000-01-01 13:30)')
{
"data": [
{
"id": 1,
"room_name": "Emerald",
"during": "[\"2000-01-01 13:00:00\",\"2000-01-01 15:00:00\")"
}
],
"status": 200,
"statusText": "OK"
}
Postgres 支持多种范围类型。您可以使用范围值的字符串表示来过滤范围列。
案例3 (关于jsonb列) link
create table
users (
id int8 primary key,
name text,
address jsonb
);
insert into
users (id, name, address)
values
(1, 'Michael', '{ "postcode": 90210, "street": "Melrose Place" }'),
(2, 'Jane', '{}');
const { data, error } = await supabase
.from('users')
.select('name')
.contains('address', { postcode: 90210 })
{
"data": [
{
"name": "Michael"
}
],
"status": 200,
"statusText": "OK"
}
-
列(column)
[必要参数]
string类型
-
值(value)
[必要参数]
object类型