Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only collection IDs from Cloud Firestore in one read count

In this document of Cloud Foreshore it is mentioned that 'a request for a list of collection IDs, you are billed for one document read.'

Now I am using AngularFire and Ionic3 for my project. My requirement is to fetch just the collection IDs from a collection with a where condition. If I do the following, it will cost me the number of reads = number of docs in the result.

let snap = this.afs.collection('path').ref
        .where('field', "==",'data').get();

snap.forEach(x => {
          list.push(x.id);
        });

I am not able to use method getCollections() which I found in a few places as a solution.

Please let me know if you have a solution.

like image 816
Tapas Mukherjee Avatar asked Dec 14 '22 09:12

Tapas Mukherjee


2 Answers

The Firestore getCollections() method only exists in the server-side SDKs, where it is charged as a single read operation. But as Doug answered, it returns the collection ids/names, not the document ids.

To get the document IDs on the client, you will need to read the entire document. So this will be charged as the number of document you read, and will consume bandwidth for all data in each document.

On the server, you can use the select() method to get a list of only the document IDs. You will still be charged for reading each of the documents, but it will consume less bandwidth.

See:

  • Firestore - Get document collections
  • How to get a list of document IDs in a collection Cloud Firestore?
like image 77
Frank van Puffelen Avatar answered May 16 '23 07:05

Frank van Puffelen


It sounds like you are wondering if there is a way to get all the document IDs in a collection without incurring a read for each document. This is currently not possible. When using Firestore client SDKs, your only option is to query the entire collection, which will transfer the entire contents of every document to the client.

like image 39
Doug Stevenson Avatar answered May 16 '23 09:05

Doug Stevenson