0
db.collection("cities").get().then(function(querySnapshot) { 
console.log(querySnapshot.size); }); 

but the problem is I am using pagination and load 30 records for each call. so I am unable to get the total numbers of records. Is there any way to get it ? like, we are doing in sql.

MichaelSolati
  • 2,847
  • 1
  • 17
  • 29
zul norrain
  • 11
  • 1
  • 5
  • You want the total number from the API or the local storage ? – Julien Kode Nov 29 '18 at 07:42
  • I understand that you want to get the TOTAL number of records of the collection. The code you mention at the beginning of your questions does exactly that. Which other number do you want? – Renaud Tarnec Nov 29 '18 at 08:04
  • There is no simple API to count documents in a collection. You will have to put some work into maintaining a document count in a scalable way. – Doug Stevenson Nov 29 '18 at 09:08
  • please can you elaborate it. " You will have to put some work into maintaining a document count in a scalable way" where can I find it? – zul norrain Nov 29 '18 at 11:11
  • @DougStevenson Hello, could you please further develop your comment on the fact that "here is no simple API to count documents in a collection.". If I am not mistaking querying the **entire** collection and using `snapshot.size` returns the number of docs in the collection. With your comment, do you mean that it may cost a lot if the collection is large and therefore it is not a recommended way? – Renaud Tarnec Nov 29 '18 at 17:29

1 Answers1

1

How to get the total number of records in a collection with Firebase ?

The method numChildren() do that. You can find more information about it here

Here is an example to use it:

db.collection("cities").get().then(function(querySnapshot) { 
    console.log(querySnapshot.numChildren());
});

I hope my answer help you

Julien Kode
  • 5,010
  • 21
  • 36
  • If I am not mistaking, the OP is using Firestore and not the Realtime Database. – Renaud Tarnec Nov 29 '18 at 07:37
  • He want to total number stored or the total on the API, sorry for my misunderstanding – Julien Kode Nov 29 '18 at 07:41
  • Indeed, IMHO, the OP question is not crystal clear but in your answer you mix up the Realtime Database with Firestore. `numChildren` is a method of the Realtime Database, not of Firestore. https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#numChildren. The Firestore equivalent is `snapshot.size`, as the OP has noticed. – Renaud Tarnec Nov 29 '18 at 07:52
  • That’s true, I think he need to tell us more information, but as you say I think I’ve misunderstood the question – Julien Kode Nov 29 '18 at 07:54
  • sorry I have missed the limit here. actually I am using load more, first time load 30 records and then on every load i am getting 30 records using "FlatList". db.collection("cities").limit(30).get().then(function(querySnapshot) { console.log(querySnapshot.numChildren()); }); here every time i am getting 30, but I need all count( total number of records) – zul norrain Nov 29 '18 at 11:10
  • Can make another query to get the number ? – Julien Kode Nov 29 '18 at 11:11
  • You want the total number of record in your FlatList or on the server ? – Julien Kode Nov 29 '18 at 12:10
  • @JulienKode on server – zul norrain Nov 29 '18 at 13:00
  • You can my solution but, do not paginante the query – Julien Kode Nov 29 '18 at 13:09
  • @JulienKode as far as I search for two days, I reached the conclusion that there is no API call to get the count from the server. there is only one way, get all the data from firestore server and find its size like (querySnapshot.docs.length).which is a big drawback of firestore. – zul norrain Nov 29 '18 at 13:30