0

I need get all documents id (only ID) without documents data in "users" colecction. It's need for save traffic data and get less documents reads in Firestore.

Fore example: I need get all numbers in collection ('users') and add they to list. They can be thousands, and I want save traffic and to get best perfomence. It's possibile for mobile sdk? enter image description here

han.dev
  • 1
  • 2
  • 1
    There is no mobile or web APIs to list document IDs without data. You have to query the entire collection and get all the data. There is only a backend API that will list IDs without data. – Doug Stevenson Oct 19 '20 at 04:59

1 Answers1

0

Small collection (less than 100 documents)

db.collection('...').get().then(snap => {
   size = snap.size // will return the collection size
});

Large Collection

You can maintain special collection to store number of users in your users collection. Whenever you need to read the count of users, you can call this special collection which keeps count of users. By doing this, you can save lot of Read operations.
When new user register into your system, you can increment the value of this special collection using

FieldValue.increment()

Firestore now allows incrementing counters, completely atomically, and without reading the data prior. This ensures we have correct counter values even when updating from multiple sources simultaneously (previously solved using transactions), while also reducing the number of database reads we perform.

  • Special Collection is new collection just used for maintaining users count and store document id of each users

Server SDK (Admin SDK)

db.collection('foo').select()

the server will send you the documents that match, but will omit all fields from the query result.

Ferin Patel
  • 3,424
  • 2
  • 17
  • 49
  • The question is not asking to count documents. The question is asking to get a list of all document IDs in a collection. – Doug Stevenson Oct 19 '20 at 05:00
  • @DougStevenson You can save users document ids in special collection in one document inside array along with its count. this can also save entire `users` collection read. – Ferin Patel Oct 19 '20 at 05:02
  • So what is your solution for small collections doing? Because it's not doing anything with document IDs. – Doug Stevenson Oct 19 '20 at 05:06
  • @DougStevenson For example you have 1ooo users records and 10 sub fields in each document. so fetching document ids for 1000 users would use more bandwidth. Instead create new collection in which it store users document ids and count in single document and fetching would require lesser bandwith – Ferin Patel Oct 19 '20 at 05:12
  • @FerinPatel maybe you're forgetting that it will cost extra write entry. – Ashish Oct 19 '20 at 05:33
  • 2023 here - Select Count(*) is now available – Spock Jan 26 '23 at 16:43