Fetch all keys from Firestore collection with admin sdk

Viewed 664

I need to fetch all the ids/keys of a collection in Cloud Firestore. Currently I do it like this (groovy):

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

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

I run this on a collection which potentially has could have a lot of documents lets say 30.000 which causes a java.lang.OutOfMemoryError: Java heap space

The thing is that I don't need all the documents. As seen in my code all I need is to check which documents are in the collection (ie. a list of keys/id's), but I have not found any way to grab them with out fetching all the documents which has a huge overhead.

I using the Java Firebase Admin SDK (6.12.2).

So I'm hoping that there is a way to grab all the keys with out the overhead and without my heap maxing out.

1 Answers

Calling get() will get you the full documents. But you should be able to do an empty selection. From the documentation for 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.

So something like this:

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

Also see:

Related