在Web JavaScript中获取Cloud Firestore集合的快照大小 [英] Getting snapshot size of cloud firestore collection in web javascript

查看:90
本文介绍了在Web JavaScript中获取Cloud Firestore集合的快照大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java技术将Cloud Firestore用作数据库来开发Web应用程序。在我的数据库中,集合中大约有40万个文档。当我编写查询以获取快照大小时,我的网站将进入调试状态,并且永远不会返回该大小。

  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("错误获取文档:" ;,错误);
});


所以,我编写了另一个替代查询,如下所示,使用带有查询游标的分页数据。

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

getProducts(productfirst);

函数getProducts(first){

first.get()。then(function(documentSnapshots){
//获取最后一个可见文件
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 {
//构造新查询
var next = db2.collection( Products)。orderBy( ProductName)。startAfter(lastVisible)
.limit(10000);从本文档开始,获取下一个25个城市。

getProducts(next);
}
});
}

此查询返回快照大小,但要花费5分钟以上才能获取总数。还有其他更快的方法来获取Firestore集合大小吗?而且,我需要将这些文档设置到数据表中。

解决方案

由于Firebase Cloud Firestore会根据文档读取次数向您收费,这将使您的账单增加到天文数字。我建议您创建一个文档来存储这些统计数据。可以使用 fieldvalue 功能与< onCreate和onDelete的a href = https://firebase.google.com/docs/functions/database-events rel = nofollow noreferrer>数据库触发器。

这里是上述云函数的实现参考

  // 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;


//创建后增加
exports.makeUppercase = functions.firestore.document( / Products / {productID})。onCreate((handler,context) => {
db.collection( statistics)。doc( products)。update({
nProducts:fieldVal.increment(1)
})
})

//因删除而减少
exports.makeUppercase = functions.firestore.document( / Products / {productID})。onDelete((handler,context)= > {
db.collection( statistics)。doc( products)。update({
nProducts:fieldVal.increment(-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);
});

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.

解决方案

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.

这篇关于在Web JavaScript中获取Cloud Firestore集合的快照大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆