How to get list of documents from a collection in Firestore Android

后端 未结 2 630
滥情空心
滥情空心 2021-01-17 18:12

My structure of Firestore database:

|
|=>root_collection
                  |
                  |=>doc1
                         |                  
            


        
2条回答
  •  一个人的身影
    2021-01-17 18:35

    To have a list that contains all the name of your documents within the root_collection, please use the following code:

    firestore.collection("root_collection").get().addOnCompleteListener(new OnCompleteListener() {
        @Override
        public void onComplete(@NonNull Task task) {
            if (task.isSuccessful()) {
                List list = new ArrayList<>();
                for (QueryDocumentSnapshot document : task.getResult()) {
                    list.add(document.getId());
                }
                Log.d(TAG, list.toString());
            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
        }
    });
    

    The result in your logcat will be:

    [doc1, doc2, doc3]
    

    Remember, this code will work, only if you'll have some properties within those documents, otherwise you'll end ut with an empty list.

提交回复
热议问题