简体   繁体   中英

How can I query only certain documents from a firestore collectiongroup, based on the value of a field?

So, I have this firestore database which stores my users and their posts. The posts have this boolean 'available' field which I want to use in order to select which posts are going to appear on my dashboard. I use collectionGroup to query all the posts, but when trying to check if the 'available' field is 'true' or 'false' using .whereEqualTo("available", "true"), my app runs but the dashboard doesn't load any of the posts and shows a Toast saying "No document found". This is my code:


    private void homePostsRetriever(Context context) {
        String userId = FirebaseAuth.getInstance().getCurrentUser() != null ? FirebaseAuth.getInstance().getCurrentUser().getUid() : null;

        if (userId == null || userId.isEmpty()) {
            Toast.makeText(context, "No user id", Toast.LENGTH_SHORT).show();
            return;
        }

        FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
        rootRef.collectionGroup("posts").whereEqualTo("available", "true").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot documentSnapshots) {
                for (DocumentSnapshot documentSnapshot : documentSnapshots.getDocuments()) {
                    Post post = new Post();
                    post.setTitle(documentSnapshot.getString("title"));
                    post.setDescription(documentSnapshot.getString("description"));
                    
                    list.add(post);
                }
                notifyDataSetChanged();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(context, "No document found", Toast.LENGTH_SHORT).show();
            }
        });
    }

It is perfectly working if I try to show all posts, not including .whereEqualTo() so this method seems to be the problem..

This is the database's structure:

user1 - post1 - postOwner(String) - 'Ted'
              - postTitle(String) - 'Hello everybody'
              - **available**(Boolean) - '**true**'

      - post2 - postOwner(String) - 'Ted'
              - postTitle(String) - 'Afsadf asdfasdf'
              - **available**(Boolean) - 'false'

user2 - post1 - postOwner(String) - 'John'
              - postTitle(String) - 'Dasdas asdfasf'
              - **available**(Boolean) - '**true**'

      - post2 - postOwner(String) - 'John'
              - postTitle(String) - 'Fasd dasdwef'
              - **available**(Boolean) - 'false'
      - post3 - postOwner(String) - 'John'
              - postTitle(String) - 'TRfdsaf fasdfar'
              - **available**(Boolean) - 'false'


UPDATE: I added the database I changed the code from "true" to true using .whereEqualTo("available", true) but I get the same result. No post loaded..

UPDATE2: I added the code img just to be clear

The type of the field has to match between both the client and the server. There's not enough information here to tell for sure (a screenshot would be better), but your query is asking for documents whose "available" field is set to the exact string "true". Note that it's asking for a string, not a boolean. If your documents actually contain boolean field values, they will not be found by the query, because the types don't match.

If your document field "availalble" actually contains booelan values instead of string, you should pass a boolean to the query instead of a string like this:

whereEqualTo("available", true)

Not like you are now, with a string value:

whereEqualTo("available", "true")  // the quotes around "true" make it a string

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