0
votes

let's say I have 3 collection 'col1' , 'col2' & 'col3' each collection consists of thousands of documents i want to push all the documents data into a single array of object according to their timestamp in a descending order. The firestore orderBy command can be used only for single collection So is there any way i can push the data from firestore in such a way that the array comes sorted by default ?

2

2 Answers

1
votes

With Firestore, you can only query documents in one collection at a time. If you are interested in documents among three collections, you will need three queries. If you want the entire set of documents to be sorted, you will have to sort them on the client after all the queries are complete.

0
votes

The most effecient approach would be to merge sorted results. Time complexity in your case will be O(n) since you have constant number of arrays.

Here is an interesting post with the explanation of the algorithm if you are interested. Otherwise, just use lodash.merge then sort it. Time complexity will remain O(n) since sorting is O(log(n)). It won't really matter unless you have millions of entries in your collections.