Firebase query by specific field value

Viewed 30

Below is the structure of the data. Now how can get the list of data where the uid is equal to ZeN7PcvLeDS8ISjXmBKelk9OUbu2.

I am using Angular 14 and firebase real time database. Following the official doc https://firebase.google.com/docs/database/web/lists-of-data (for crud)

enter image description here

I was trying to achieve this in this way. Any kind of help is highly appreciated. Also above structure of the data is correct according to the types of data?

getall() {
  let uid = this.auth.currentUser()?.uid;
  const db = getDatabase();
  return query(ref(db, 'expenses/' + uid), orderByChild('uid')); 
}
1 Answers

To get the expenses of a specific UID, you can do:

query(
  ref(db, 'expenses'), 
  orderByChild('uid'), 
  equalTo('ZeN7PcvLeDS8ISjXmBKelk9OUbu2')
)

So if uid has the correct value in your code, that'd be:

query(
  ref(db, 'expenses'), 
  orderByChild('uid'), 
  equalTo(uid)
)
Related