Search code examples
rxjsgoogle-cloud-firestorerxjs6angularfire5

Using .groupBy with Firestore collection data


I have a Firestore collection with a number of jobs but I want to group them by the job number on the client side. I have gone through a lot of examples online like this.jobs = this.afs.collection('jobs').valueChanges().pipe(groupBy((job: any) => job.jobNumber), mergeMap(group => group.pipe(toArray()))); but no success. What I have is

Job - job number 1 - job number 2 - job number 3 - job number 1

But what i want is

Job - [job number 1, job number 1] - [job number 2] - [job number 3]


Solution

  • the problem is that when you subscribe to valueChanges() it becomes hot observable and never completes, so you can;t get result as you want - how stream should know when all job number 1 arrived? Some workarounds could be done though, f.ex. add a timeout and with scan collect all values in accumulator Subjects etc. then merge those and emit.

    But does it makes sense if you already storing all data locally? Probably not. IMO, you can only work with response after it arrived, f.ex. create subjects and depending on job number 1 call next() on them.