简体   繁体   中英

How do I query Firestore Array Value?

How can I Query the Array dates with the month value? 在此处输入图片说明

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?

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
               }
           }
        }
    );
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) {

        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM