0

I'm trying to query my database for an android application, if a username is in the 'fave' array field of my database and if so then change the background of an image. My database is set up like this...

WHERE THE FIELD IS IN MY DATABASE

I don't know if i'm doing it right but currently i think i may be checking the whole collection rather than a specific document and it's not even returning anything. Any suggestions would be appreciated!

5
  • I suggest starting with the documentation to learn how to do queries. firebase.google.com/docs/firestore/query-data/queries Commented Apr 9, 2020 at 18:27
  • @DougStevenson sorry maybe i should have been more clear. In the documentation the query objects search a whole collection but i only want to search a specific document, as the user can be in multiple trainers 'fave' arrays Commented Apr 9, 2020 at 18:32
  • Then just get() the document you're looking for. firebase.google.com/docs/firestore/query-data/get-data Commented Apr 9, 2020 at 18:33
  • @DougStevenson if i add the .document() path afterwards i can no longer use the .whereInArray()_ Commented Apr 9, 2020 at 18:33
  • Yes, you can't filter when identifying a specific document. There's no need to - you already know the document ID. Commented Apr 9, 2020 at 18:52

1 Answer 1

1

You wanted this?

FirebaseFirestore.getInstance().collection("Trainers").document("Air Force 1 Low").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
        List<String> list = (List<String>)documentSnapshot.get("fave");
        for (String name : list){
            if (name.equals("Admin")){
                //do if user is admin
                return;
            }
        }
        //do if user is not admin
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Top lad. I've been struggling to get my head round how to query documents but this has helped a lot. Thanks!

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.