[英]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.