abuka123
abuka123

Reputation: 451

How to retrieve all document ids from a collection?

I'm trying to get all of the document ids in a collection groups. The document id is the group name which is unique. Also each document in groups does not have fields, only pointers to other collections. I wrote the following code:

fireDB.collection("groups").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            QuerySnapshot querySnapshots = task.getResult();
            if (querySnapshots != null) {
                for (QueryDocumentSnapshot currentDocumentSnapshot : querySnapshots) {
                    groups.add(currentDocumentSnapshot.getId());
                }
                Collections.sort(groups);
                ArrayAdapter<String> adapter = new ArrayAdapter<>(SignUpActivity.this,android.R.layout.simple_list_item_1,groups);
                groupNameText.setAdapter(adapter);
            } else {
                Log.d(this.getClass().getName(), "No groups in the database");
            }
        } else {
            Log.d(this.getClass().getName(), "addOnCompleteListener:failed");
        }
    }
});

But groups is always empty because firebase does not give me documents without fields (figured it out after some debugging). How should I do it?

Upvotes: 0

Views: 142

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598901

I sounds like you have some subcollections under document paths, without having a document at that path. This is a valid situation, but I don't think it is possible to get those locations in the client-side SDKs, as there is no document there.

See https://www.reddit.com/r/Firebase/comments/b0poug/empty_virtual_docs_this_document_does_not_exist/

The Firebase console shows these locations in italics, since it needs to show the subcollections under each location. It likely uses the show_missing flag in the REST API or Admin SDK for that.

Upvotes: 2

Related Questions