1

I am developing a web application with javascript using a cloud firestore as a database. In my database, I have around 400 000 documents in a collection. when I wrote a query to get the snapshot size my website is going to debugging state and will never return the size.

db2.collection("Products").get().then(function(querySnapshot) {
   querySnapshot.forEach(function(doc) {
      // console.log(doc.id, " => ", doc.data());
   });
   console.log("size",querySnapshot.size);
})
.catch(function(error) {
   console.log("Error getting documents: ", error);
});

enter image description here

So, I wrote another alternative query like below using paginate data with query cursors.

var productfirst = db2.collection("Products").orderBy("ProductName").limit(10000);

getProducts(productfirst);
    
function getProducts(first){
    
   first.get().then(function (documentSnapshots) {
       // Get the last visible document
       pcount = pcount + documentSnapshots.size;
       console.log("products",documentSnapshots.size,pcount);
       var lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1];
    
       document.getElementById('totalProducts').textContent = pcount;
    
       if(documentSnapshots.size == 0){
           document.getElementById('totalProducts').textContent = pcount;
           // document.getElementById("loadspinner").style.display = "none";
       }
       else {
           // Construct a new query starting at this document, get the next 25 cities.
           var next = db2.collection("Products").orderBy("ProductName").startAfter(lastVisible)
                      .limit(10000);
    
           getProducts(next);
       }
    });
}

This query returning the snapshot size but it's taking more than 5 minutes to get the total count. Is there any other faster way to get the firestore collection size? And also, I need to set these documents to the data tables.

James Z
  • 12,209
  • 10
  • 24
  • 44
Lakshmi
  • 278
  • 5
  • 19
  • As CRUD DS answered, this is not a good approach to determine the document count. It downloads all documents (which probably explains why it takes so much time) and then throws them all away only to return the count. To efficiently get the number of documents, you have to store that count in the database. See https://stackoverflow.com/questions/46554091/cloud-firestore-collection-count/49407570#49407570 and https://firebase.google.com/docs/firestore/solutions/counters – Frank van Puffelen Aug 04 '20 at 13:54

1 Answers1

1

Since Firebase Cloud Firestore charges you based on document reads, this will rack up your bill to astronomical numbers. I would suggest you create a document to store these statistics. It can be done using the fieldvalue functionality paired with the database triggers of onCreate and onDelete.

Here are the implementation reference for said cloud functions

// IMPORT
const functions = require('firebase-functions');
const admin = require('firebase-admin');

// INIT
const firebaseApp = admin.initializeApp(yourConfig)
var db = firebaseApp.firestore();
var fieldVal = admin.firestore.FieldValue;


// INCREMENT UPON CREATION
exports.makeUppercase = functions.firestore.document("/Products/{productID}").onCreate((handler, context)=>{
    db.collection("statistics").doc("products").update({
        nProducts: fieldVal.increment(1)
    })
})

// DECREMENT UPON DELETION
exports.makeUppercase = functions.firestore.document("/Products/{productID}").onDelete((handler, context)=>{
    db.collection("statistics").doc("products").update({
        nProducts: fieldVal.increment(-1)
    })
})

Alternatively, you can also use said increment and decrement functions on your product management panel to rid of the need for cloud functions. Just ensure you have created the statistics document beforehand.

CRUD DS
  • 456
  • 2
  • 5
  • Do we need to store the total count? Is there any other way to get the collection size at a time instead of reading all the documents. – Lakshmi Aug 05 '20 at 04:41