Home > Software engineering >  Firebase Realtime Database equalTo query for child in increasing keys
Firebase Realtime Database equalTo query for child in increasing keys

Time:10-10

I'm trying to fetch data from database. But after i changed my code for performance issues on my react-native project, i couldn't find the query for my desire. The deal is i have this type data structure. it goes under /root/news/0/coin_name. 0 key is increasing like 1,2,3...n for each of my data. for better understanding:

cnh
I
I
--coins
|
|
--news|
      |
      |--0|
          --coin_name='algorand'
          ...
      |--1|
          ...

I want to fetch datas where coin_name equals to such as 'algorand'. I tried some queries but none of them worked. I'd be appreciated if you help me.

i tried:

useEffect(()=>{
        const fbdb = async () =>{
            await database()
      .ref(newsKey)
      .orderByChild('year')
      .equalTo('2021')
      .once('value')
      .then(snapshot => {
        const data = snapshot.val();
        const formattedResponse=formatMarketData(data,show);
        return setNews(formattedResponse)
      });
      };
    
        fbdb();
       
    },[show,year]);

CodePudding user response:

I think you're looking for

await database()
  .ref('news')
  .orderByChild('coin_name')
  .equalTo('algorand')

Note though that these numeric, sequential keys are typically an anti-pattern in Firebase. To learn why, and a more idiomatic structure with push keys, read Best Practices: Arrays in Firebase.

CodePudding user response:

After all my research on web, i've concluded that firebase realtime database api does not support nested queries with unknown paths. I should use another structure or filter data after fetching all the data(which is highly inefficient for large datasets). So i decided to try using firestore for news only without a news tag. I'll let you know if that way does the trick.

  • Related