简体   繁体   English

在多个 firestore 集合中使用 orderby

[英]Use orderby in more than one firestore collection

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.假设我有 3 个集合“col1”、“col2”和“col3”,每个集合包含数千个文档,我想根据时间戳按降序将所有文档数据推送到 object 的单个数组中。 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? firestore orderBy 命令只能用于单个集合 那么有什么方法可以从 firestore 推送数据,使数组默认排序?

With Firestore, you can only query documents in one collection at a time.使用 Firestore,您一次只能查询一个集合中的文档。 If you are interested in documents among three collections, you will need three queries.如果您对三个 collections 中的文档感兴趣,则需要三个查询。 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.如果您希望对整个文档集进行排序,则必须在所有查询完成后在客户端对它们进行排序。

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.在您的情况下,时间复杂度将为 O(n),因为您有恒定数量的 arrays。

Here is an interesting post with the explanation of the algorithm if you are interested.如果您有兴趣,这是一个有趣的帖子,其中包含算法的解释。 Otherwise, just use lodash.merge then sort it.否则,只需使用lodash.merge然后对其进行排序。 Time complexity will remain O(n) since sorting is O(log(n)).时间复杂度将保持 O(n),因为排序是 O(log(n))。 It won't really matter unless you have millions of entries in your collections.除非您的 collections 中有数百万个条目,否则这并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM