最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Supabase JSON query JavaScript - Stack Overflow

programmeradmin5浏览0评论

Im trying to fetch single entry from my table that contains and JSONB array of objects. Can I match somehow that array to find the desired result?

[
    {
        "chats": [
            { "id":  56789 },
            { "id":  66753 },
        ],
        "id": 999
    },  
    {
        "chats": [
            { "id":  43532 }
        ],
        "id": 999
    }
]

I would like to get the object that matches id 999 and contains in chats -> id: 66753

Tried few approaches but none worked.

I though something link this will work.But no success

let { data, error } = await supabase
  .from('xyz')
  .select('*')
  .eq('id', 999)
  .contains('chats', {id: 66753})

Can it be done ?

Im trying to fetch single entry from my table that contains and JSONB array of objects. Can I match somehow that array to find the desired result?

[
    {
        "chats": [
            { "id":  56789 },
            { "id":  66753 },
        ],
        "id": 999
    },  
    {
        "chats": [
            { "id":  43532 }
        ],
        "id": 999
    }
]

I would like to get the object that matches id 999 and contains in chats -> id: 66753

Tried few approaches but none worked.

I though something link this will work.But no success

let { data, error } = await supabase
  .from('xyz')
  .select('*')
  .eq('id', 999)
  .contains('chats', {id: 66753})

Can it be done ?

Share Improve this question edited May 2, 2023 at 5:26 dshukertjr 18.8k11 gold badges70 silver badges101 bronze badges asked Jul 16, 2021 at 8:50 MugetsuMugetsu 1,9882 gold badges26 silver badges48 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

I believe you need to use the ->> operator when querying JSONB data from supabase as noted here in the docs.

So, if your column with the array of objects is titled jsonb, something to the effect of:

  let { data, error } = await supabase
      .from('xyz')
      .select('*')
      .eq('id:jsonb->>id', 999)
      .contains('chats:jsonb->>chats', ['chats->id: 66753'])

More info can be found on PostgREST docs here

发布评论

评论列表(0)

  1. 暂无评论