2

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
iCediCe
  • 1,672
  • 1
  • 15
  • 32

1 Answers1

2

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:

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • This was exactly what I needed. No more overhead and excessive memory usage. I guess I still pay for reads for each document even though it's only the id I need, but I don't se any way around this. – iCediCe May 13 '20 at 06:37
  • 1
    I ended up with this: firestoreClient.database.collection(bucket).select().get().get().documents.collect{ it.id } which works perfectly – iCediCe May 13 '20 at 06:43
  • Thanks for confirming! – Frank van Puffelen May 13 '20 at 14:18