Supabase – 检查 PostgreSQL 的 JSON 数组是否包含数字

huangapple go评论47阅读模式
英文:

Supabase- check if a Postgres JSON array contains a number

问题

I have a jsonb column named 'units' that contains data like: {"units": [123, 220]}

I want to select the row if any number in the array matches the unit parameter

async fetchAccount(unit: number) {
  try {
    let { data, error } = await this.supabase
      .from('accounts')
      .select('*')
      .eq('units:jsonb->units', '[' + unit + ']').single();  
  } catch (error) {   
  }
}

Returns 0 rows. I tried 'contains' rather than eq with the same result.

英文:

I have a jsonb column named 'units' that contains data like: {"units": [123,220]}

I want to select the row if any number in the array matches the unit parameter

async fetchAccount(unit: number) {
  try {
    let { data, error } = await this.supabase
      .from('accounts')
      .select('*')
      .eq('units:jsonb->units', '[' + unit +']').single();  
  } catch (error) {   
  }
}

Returns 0 rows. I tried 'contains' rather than eq with same result.

答案1

得分: 1

"Conatins should work for this" should be translated as "这里应该适用 contains:"

"units->units to access the units elements within the units column, and [unit] to find arrays that include all items in the array." should be translated as "units->units 用于访问 units 列中的 units 元素,而 [unit] 用于查找包含数组中所有项的数组。"

英文:

Conatins should work for this:

async fetchAccount(unit: number) {
  try {
    let { data, error } = await this.supabase
      .from('accounts')
      .select('*')
      .contains('units->units', [unit])
      .single();  
  } catch (error) {   
  }
}

units->units to access the units elements within the units column, and [unit] to find arrays that include all items in the array.

huangapple
  • 本文由 发表于 2023年4月19日 16:12:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定