'Angular firestore perform another query from previous query

I using @angular/fire to query data and I want to saving the firebase cost by reducing the number call of firebase query.

I have 2 function that query from the same collection. First is get all data and second is query with condition. How the second function to query data from the first function so it only call 1 time if firebase query

constructor(private _afs: AngularFirestore)

function 1 get all data from user

this._afs.collection('firebase-user-table').valueChanges().subscribe(data => {
        console.log('User data:', data);
      })

function 2 get only which user is_admin is true

this._afs.collection('firebase-user-table', (ref) => ref.where(is_admin, '==', true)).valueChanges().subscribe(data => {
        console.log('Admin data:', data);
      })


Solution 1:[1]

You could do something like this using rxjs:

let allUsers$ = this._afs.collection('firebase-user-table').valueChanges();

let adminUsers$ = allUsers$.pipe(
map((users) => users.filter(user)) => user.is_admin)
)

Then subscribe to each observable as needed

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gil