javascript - How do I select rows that match any value inside an array in supabase? - Stack Overflow

I want to select all rows that match at least one of given tags from this table:If I want to search by

I want to select all rows that match at least one of given tags from this table:

If I want to search by tag1 and tag3 it should return 1st, 3rd and 4th row and when I want to search by tag2 and tag4 it should return all of them because they all have this tag and if I want to search by tag4 it should return just 2nd row.

I found how to select ones that have all the tags provided which is:

.select().contains("tags", ["tag1", "tag2"])

but the returned rows meet all two tags at once so it returns just 2nd and 3rd row and I want it to return 4th row as well.

I want to select all rows that match at least one of given tags from this table:

If I want to search by tag1 and tag3 it should return 1st, 3rd and 4th row and when I want to search by tag2 and tag4 it should return all of them because they all have this tag and if I want to search by tag4 it should return just 2nd row.

I found how to select ones that have all the tags provided which is:

.select().contains("tags", ["tag1", "tag2"])

but the returned rows meet all two tags at once so it returns just 2nd and 3rd row and I want it to return 4th row as well.

Share Improve this question edited Feb 16, 2023 at 21:25 pelak asked Feb 16, 2023 at 19:01 pelakpelak 681 silver badge7 bronze badges 2
  • Array.some() is one such function which return true if there is at least one element matching. Check this documentation – shanmukha vangaru Commented Feb 16, 2023 at 19:12
  • I would rather know how to do it straight from supabase but I think it will do just fine if I make the logic behind it good enough – pelak Commented Feb 16, 2023 at 19:19
Add a ment  | 

2 Answers 2

Reset to default 6

I think you're looking for the overlap filter https://supabase./docs/reference/javascript/overlaps

const { data, error } = await supabase
  .from('your_table')
  .select()
  .overlaps('tags', ['tag1', 'tag2'])

If you want to match any value of that array, then you need to encapsulate it within the OR filter:

const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const { data: ret, error } = await supabase
  .from('test_tags')
  .select()
  .or('tags.cs.{tag1}','tags.cs.{tag2}');
  
console.log(ret);

You can also expand and use two or more OR logical operators:

const { data: ret, error } = await supabase
  .from('test_tags')
  .select()
  .or('tags.cs.{tag1},or(tags.cs.{tag4},tags.cs.{tag3})');
  
console.log(JSON.stringify(ret));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744308368a4567828.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信