Select random document from Firestore

后端 未结 5 1587
囚心锁ツ
囚心锁ツ 2020-11-27 07:40

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

Say for example: Students is a collection in

5条回答
  •  佛祖请我去吃肉
    2020-11-27 08:12

    As per Alex's answer I got hint to get duplicate records from Firebase Firestore Database (Specially for small amount of data)

    I got some problems in his question as follow:

    • It gives all the records same as randomNumber is not updated.
    • It may have duplicate records in final list even we update randomNumber everytime.
    • It may have duplicate records which we are already displaying.

    I have updated answer as follow:

        FirebaseFirestore database = FirebaseFirestore.getInstance();
        CollectionReference collection = database.collection(VIDEO_PATH);
        collection.get().addOnCompleteListener(new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task task) {
                if (task.isSuccessful()) {
                    List videoModelList = new ArrayList<>();
                    for (DocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
                        VideoModel student = document.toObject(VideoModel.class);
                        videoModelList.add(student);
                    }
    
                    /* Get Size of Total Items */
                    int size = videoModelList.size();
                    /* Random Array List */
                    ArrayList randomVideoModels = new ArrayList<>();
                    /* for-loop: It will loop all the data if you want 
                     * RANDOM + UNIQUE data.
                     * */
                    for (int i = 0; i < size; i++) {
                        // Getting random number (inside loop just because every time we'll generate new number)
                        int randomNumber = new Random().nextInt(size);
    
                        VideoModel model = videoModelList.get(randomNumber);
    
                        // Check with current items whether its same or not
                        // It will helpful when you want to show related items excepting current item
                        if (!model.getTitle().equals(mTitle)) {
                            // Check whether current list is contains same item.
                            // May random number get similar again then its happens
                            if (!randomVideoModels.contains(model))
                                randomVideoModels.add(model);
    
                            // How many random items you want 
                            // I want 6 items so It will break loop if size will be 6.
                            if (randomVideoModels.size() == 6) break;
                        }
                    }
    
                    // Bind adapter
                    if (randomVideoModels.size() > 0) {
                        adapter = new RelatedVideoAdapter(VideoPlayerActivity.this, randomVideoModels, VideoPlayerActivity.this);
                        binding.recyclerView.setAdapter(adapter);
                    }
                } else {
                    Log.d("TAG", "Error getting documents: ", task.getException());
                }
            }
        });
    

    Hope this logic helps to all who has small amount of data and I don't think It will create any problem for 1000 to 5000 data.

    Thank you.

提交回复
热议问题