问题
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