Ben
Ben

Reputation: 1849

Query for document in subcollection firestore

I have the following structure of my database:

enter image description here

Inside the Location collection there is a document and in it there is Lat, Lon coordinates:

enter image description here

My final goal is to get all of the Lat, Lon coordinates of all users however im already stuck on how to get to Location collection if I dont know the document name where it is?

Is there some query that I can do to move straight inside to something like WhereEqualTo("Lat")?

To begin I tried the following:

    db.collection("Users").get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {

                        List<String> listID = new ArrayList<>();
                        for (QueryDocumentSnapshot doc : task.getResult()) {

                            listID.add( doc.getId() );
                        }

However it returns me null list.

Thank you

Upvotes: 0

Views: 55

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83181

You need to use a Collection Group Query, as follows:

db.collectionGroup("Location").get()
        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                // ...
            }
        });

This runs a query across all collections names Location in your database.

Upvotes: 1

Related Questions