简体   繁体   English

如何在 Firestore 中将文档分组?

[英]How do I group documents together in Firestore?

I have an ArrayList<String> ids which I can use to get documents inside a collection.我有一个ArrayList<String> ids ,我可以用它来获取集合中的文档。 For each ID in the array list of ids, I can do:对于 ids 数组列表中的每个 ID,我可以这样做:

 DocumentReference doc = fstore.collection("Collection").document(id);

For a normal query you'd use:对于普通查询,您将使用:

 Query query = fstore.collection("Collection").whereEqualTo("field", value).orderBy("Field", Query.Direction.DESCENDING).limit(5);

And then you can use get() to get the documents.然后你可以使用get()来获取文件。

My question is how do I group these documents together so I can perform operations like limit orderBy and other methods available for collection queries.我的问题是如何将这些文档组合在一起,以便我可以执行limit orderBy和其他可用于集合查询的方法等操作。

The simplest solution I can think of would be to save each operation you perform into a Task object and add each object to a List.我能想到的最简单的解决方案是将您执行的每个操作保存到任务 object 中,并将每个 object 添加到列表中。 As soon as the iteration completes, pass that list to Tasks#whenAllSuccess(Collection> tasks) method, as you can see in the following lines of code:一旦迭代完成,将该列表传递给Tasks#whenAllSuccess(Collection> tasks)方法,如您在以下代码行中所见:

List<Task<DocumentSnapshot>> tasks = new ArrayList<>();
for (String id : ids) {
    Task<DocumentSnapshot> task = db.collection("Collection").document(id).get();
    tasks.add(task);
}
Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener<List<Object>>() {
    @Override
    public void onSuccess(List<Object> list) {
        //Do what you need to do with your list
        for (Object object : list) {
            ModelClass mc = ((DocumentSnapshot) object).toObject(ModelClass.class);
        }
    }
});

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

相关问题 如何阻止 firestore web 客户端根据文档内容阅读规则中的某些文档? - How do I stop a firestore web client from reading certain documents in rules based off a documents content? Firestore Group Collection 文件的唯一性 - Firestore Group Collection documents uniqueness 如何从 firestore 一次获取多个集合组的文档? - How to get multiple collection group's documents at once from firestore? 如何使用模块 Web SDK 的版本 9 获取 Cloud Firestore 集合中的所有文档? - How do I get all documents in a Cloud Firestore collection using Version 9 of the Modular Web SDK? 如何检索作为文档放置在 Firebase Cloud Firestore 中的 UID - How do I retrieve the UID's that I have placed as documents in my Firebase Cloud Firestore 如何将文档上传到 Firestore - How to upload documents to Firestore 我如何知道是否还有更多文档需要从 Firestore 集合中获取? - How do I know if there are more documents left to get from a firestore collection? 如何从 firestore 集合中获取文档列表 - How do you fetch a list of documents from a firestore collection 如何使用 Flutter、Firebase - Firestore 数据库根据日期对数据进行分组和列出? - How do I Group and List data according to date using Flutter, Firebase - Firestore Database? 如何计算 swift 中 firestore 查询的文档数? - How can I count the documents from a firestore query in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM