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.