簡體   English   中英

如何從 Firestore Android 中的集合中獲取文檔列表

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

我的 Firestore 數據庫結構:

|
|=>root_collection
                  |
                  |=>doc1
                         |                  
                         |=>collection
                  |
                  |=>doc2
                         |                  
                         |=>collection
                  |
                  |=>doc3
                         |                  
                         |=>collection

現在我想從root_collection獲取文檔列表。 會有一個包含以下數據的列表{"doc1", "doc2", "doc3"} 我需要它,因為我想制作一個微調器並將這些數據放入微調器中。 然后用戶將選擇一些文件並下載它。

我嘗試使用下面的代碼:

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

但是代碼只有在我擁有文檔中沒有集合的數據結構時才有效。 在其他情況下, QueryDocumentSnapshot中沒有任何文檔。

謝謝!

要在root_collection中包含包含所有文檔名稱的root_collection ,請使用以下代碼:

firestore.collection("root_collection").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            List<String> 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());
        }
    }
});

您的 logcat 中的結果將是:

[doc1, doc2, doc3]

請記住,此代碼將起作用,僅當您在這些文檔中具有某些屬性時,否則您將以空列表結束 ut。

您可以調用 collection 方法獲取 root_collections 中的文檔,然后保存文檔 ID,稍后將用於獲取文檔的集合。

創建根集合對象為:

    data class RootCollection(
        @DocumentId val id: String,
        val sampleField: String?
    ) {
       // empty constructor to allow deserialization
       constructor(): this(null, null)
    }

然后使用 FirebaseFirestore 方法集合獲取集合,如下所示:

    val querySnapshot = firestore.collection("root_collection")
                .get()
                .await()
    if(!querySnapshot.isEmpty) {
             Result.SUCCESS(querySnapshot.toObjects(RootCollection::class.java))
     } else {
         Result.ERROR(Exception("No data available"))
    }

然后在文檔調用中獲取集合

    firestore.collection("root_collection/$documentId/collection") 

請注意,我使用了此鏈接中描述的 kotlin 協程等待方法

Result 類用於保存返回數據的狀態並處理錯誤。

   sealed class Result<out T: Any> {
      data class SUCCESS<out T: Any>(val data: T) : Result<T>()
      data class ERROR(val e: Exception): Result<Nothing>()
      object LOADING : Result<Nothing>()
   }

好吧,我認為你現在不能這樣做,因為firestore沒有提供這些關於子文檔的知識。 您的Root Collection將只知道其文檔。

但是你可以做的是在你的Root集合的文檔中獲取對你的集合的引用,然后你可以獲得該集合中的文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM