Getting snapshot size of cloud firestore collection in web javascript [duplicate]

杀马特。学长 韩版系。学妹 提交于 2020-08-10 19:07:07

问题


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);
});

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.


回答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.



来源:https://stackoverflow.com/questions/63244534/getting-snapshot-size-of-cloud-firestore-collection-in-web-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!