从Firestore中选择随机文档 [英] Select random document from Firestore

查看:65
本文介绍了从Firestore中选择随机文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Cloud Firestore的单个集合中有1000个文档,是否可以提取随机文档?

I have 1000 documents in a single collection in Cloud Firestore, is it possible to fetch random documents?

例如:Students是Firestore中的一个集合,我有1000个学生,我的要求是每次通话随机选择10个学生.

Say for example: Students is a collection in Firestore and I have 1000 students in that collection, my requirement is to pick 10 students randomnly on each call.

推荐答案

是的,要实现此目的,请使用以下代码:

Yes it is and to achieve this, please use the following code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference studentsCollectionReference = rootRef.collection("students");
studentsCollectionReference.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            List<Student> studentList = new ArrayList<>();
            for (DocumentSnapshot document : task.getResult()) {
                Student student = document.toObject(Student.class);
                studentList.add(student);
            }

            int studentListSize = studentList.size();
            List<Students> randomStudentList = new ArrayList<>();
            for(int i = 0; i < studentListSize; i++) {
                Student randomStudent = studentList.get(new Random().nextInt(studentListSize));
                if(!randomStudentList.contains(randomStudent)) {
                    randomStudentList.add(randomStudent);
                    if(randomStudentList.size() == 10) {
                        break;
                    }
                }
            }
        } else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});

这称为经典解决方案,您可以将其用于仅包含少量记录的集合中,但是如果您担心获取大量读取,那么,我建议您使用第二种方法.这还涉及通过添加新文档来对数据库进行一些更改,该文档可以包含具有所有学生ID的数组.因此,要随机抽取10个学生,您只需进行get()调用即可,这意味着仅需一次读取操作.一旦获得该数组,就可以使用相同的算法并获得这10个随机ID.一旦有了这些随机ID,就可以获取相应的文档并将其添加到列表中.这样,您仅执行10次读操作即可获得实际的随机学生.总共只有11次文档读取.

This is called the classic solution and you can use it for collections that contain only a few records but if you are afraid of getting huge number of reads then, I'll recommend you this second approach. This also involves a little change in your database by adding a new document that can hold an array with all student ids. So to get those random 10 students, you'll need to make only a get() call, which implies only a single read operation. Once you get that array, you can use the same algorithm and get those 10 random ids. Once you have those random ids, you can get the corresponding documents and add them to a list. In this way you perform only 10 more reads to get the actual random students. In total, there are only 11 document reads.

这种做法称为非规范化(复制数据),是涉及Firebase的常见做法.如果您不是NoSQL数据库的新手,为了更好地理解,我建议您观看以下视频,使用Firebase数据库进行反规范化是正常的.它用于Firebase实时数据库,但相同的原理也适用于Cloud Firestore.

This practice is called denormalization (duplicating data) and is a common practice when it comes to Firebase. If you're new to NoSQL database, so for a better understanding, I recommend you see this video, Denormalization is normal with the Firebase Database. It's for Firebase realtime database but same principles apply to Cloud Firestore.

但是请记住,您将随机产品添加到这个新创建的节点中的方式与您在不再需要它们时需要将其删除的方式相同.

But rememebr, in the way you are adding the random products in this new created node, in the same way you need to remove them when there are not needed anymore.

要将学生ID添加到数组中,只需使用:

To add a student id to an array simply use:

FieldValue.arrayUnion("yourArrayProperty")

要删除学生证,请使用:

And to remove a student id, please use:

FieldValue.arrayRemove("yourArrayProperty")

要一次获得所有10个随机学生,可以使用List<Task<DocumentSnapshot>>,然后调用Tasks.whenAllSuccess(tasks),如我在这篇帖子中的回答所述:

To get all 10 random students at once, you can use List<Task<DocumentSnapshot>> and then call Tasks.whenAllSuccess(tasks), as explained in my answer from this post:

这篇关于从Firestore中选择随机文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆