使用 admin sdk 从 Firestore 集合中获取所有密钥

Fetch all keys from Firestore collection with admin sdk

我需要获取 Cloud Firestore 中集合的所有 ids/keys。目前我是这样做的 (groovy):

ApiFuture<QuerySnapshot> snapshot = firestoreClient.database.collection(bucket).get()

List<QueryDocumentSnapshot> documents = snapshot.get().getDocuments()
for (QueryDocumentSnapshot document : documents) {
    keys.add(document.id)
}

我 运行 这在一个可能有很多文档的集合上,假设 30.000 导致 java.lang.OutOfMemoryError: Java heap space

问题是我不需要所有文件。正如在我的代码中看到的那样,我只需要检查集合中有哪些文档(即 keys/id 的列表),但我还没有找到任何方法来获取它们而不获取所有具有的文档巨大的开销。

我使用 Java Firebase Admin SDK (6.12.2)。

所以我希望有一种方法可以获取所有密钥,而不会产生开销,也不会耗尽我的堆。

致电 get() 将为您提供完整的文件。但是你应该能够做一个空的选择。来自 select() 的文档:

public Query select(String... fields)

Creates and returns a new Query instance that applies a field mask to the result and returns the specified subset of fields. You can specify a list of field paths to return, or use an empty list to only return the references of matching documents.

所以像这样:

firestoreClient.database.collection(bucket).select(new String[0])

另见: