0

How can I Query the Array dates with the month value?enter image description here

String monthString = "12";
Query queryZero = db.collection("Users").document(mCurrentUser).collection("Dates").whereArrayContainsAny("dates", ???);

What do I have to put where the '???' to retrieve dates with the dd/MM/yyyy <- /MM/ value is equal to the monthString?

2 Answers 2

2

Firestore at the moment does not support this kind of query. But, a possible workaround is to store additional array of months in your document and perform: db.collection("Users").document(mCurrentUser).collection("Dates").whereArrayContains("months", "12");

Another solution is similar to @Ruyut's answer. But this would retrieve all the documents in the collection and you would have to perform the filtering in the client-side which could possibly degrade performance if you have thousands of documents.

 FirebaseFirestore.getInstance().collection("Users")
    .get()
    .addOnSuccessListener(
        new OnSuccessListener<QuerySnapshot>() {
           @Override
           public void onSuccess(QuerySnapshot documentSnapshots) {
               for (DocumentSnapshot ds : documentSnapshots.getDocuments()) {
                   // same code as @Ruyut's answer
               }
           }
        }
    );
Sign up to request clarification or add additional context in comments.

1 Comment

Second option is not ideal as in this case due to high amount of documents as you've mentioned, but I appreciate your help. In the end I've implemented a different adapter logic which checks the current day and goes X days back and checks those values.
1
public static void getData(){
    DatabaseReference database = FirebaseDatabase.getInstance().getReference("Users").child(mCurrentUser).child("Dates");
    database_course.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                String key = ds.getKey(); //7UE......
                HashMap<String, ArrayList<String>> datesHashMap = ds.get(key);
                for(int i =0;i<datesHashMap.get("dates").size();i++){
                    String date = datesHashMap.get("dates").get(i);//08/12/2019
                    if (date.substring(3,5).equals("12")){
                        //put your code
                    }
                }

            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

1 Comment

This is a flawless approach for a Firebase database but I am trying to apply this logic into a Query for Firestore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.