Dor Furman
Dor Furman

Reputation: 55

Firestore read counts of documents size

I'm trying to get the amount of documents in a specific collection as the following :

const userCol = db.collection('users').get().then(queryResult => {
   if (queryResult.exists)
       console.log(queryResult.size); // amount of documents
});

So as I use .get() to read the documents that means that I get x reads of the amount of documents exists? If that's true then is there a way to just get the amount of documents without having to read every each one of them?

Upvotes: 0

Views: 47

Answers (1)

Ferhat Ismayilov
Ferhat Ismayilov

Reputation: 273

First, the amount of read will increase only 1 time in the function you wrote. That is, it does not depend on the number of documents. It depend from the number of times use the get() function. Each use of the get() function means 1 read count. You will access all documents in the users collection, but the amount of read will increase by 1 because you only use get() once.

Note: The definition of .exist applies to the document, not the collection. With the exist command you can poll for the existence of the document, not the collection.

const userCol = db.collection('users').get().then(queryResult => {
   console.log(queryResult.size); // amount of documents
});

or you can also use .listDocuments() property for get documents list in the collection.

const userCol = db.collection('users').listDocuments().then(documentsList => {
   console.log(documentsList.length); // amount of documents
});

Both ways will increase the number of 1 reads.

Upvotes: 2

Related Questions