Cloud Firestore集合计数 [英] Cloud Firestore collection count

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

问题描述

是否可以使用新的Firebase数据库Cloud Firestore计算集合中有多少项?

Is it possible to count how many items a collection has using the new Firebase database, Cloud Firestore?

如果是,该怎么办?

推荐答案

与许多问题一样,答案是-这取决于.

As with many questions, the answer is - It depends.

在前端处理大量数据时,您应该非常小心. Firestore除了使您的前端呆滞之外,还向您收取每百万次读取0.60美元的费用您做出的.

You should be very careful when handling large amounts of data on the front end. On top of making your front end feel sluggish, Firestore also charges you $0.60 per million reads you make.

谨慎使用-前端用户体验可能会受到打击

只要您对返回的数组不做太多逻辑,就可以在前端进行处理.

Handling this on the front end should be fine as long as you are not doing too much logic with this returned array.

db.collection('...').get().then(snap => {
   size = snap.size // will return the collection size
});


媒介收藏(100至1000个文档)

谨慎使用-Firestore读取调用可能会花费很多


Medium collection (100 to 1000 documents)

Use with care - Firestore read invocations may cost a lot

在前端处理此问题是不可行的,因为它有太多降低用户系统速度的潜力.我们应该处理此逻辑服务器端,只返回大小.

Handling this on the front end is not feasible as it has too much potential to slow down the users system. We should handle this logic server side and only return the size.

此方法的缺点是您仍在调用Firestore读取(等于集合的大小),从长远来看,这样做可能会使您花费比预期更多的钱.

The drawback to this method is you are still invoking firestore reads (equal to the size of your collection), which in the long run may end up costing you more than expected.

云功能:

...
db.collection('...').get().then(snap => {
    res.status(200).send({length: snap.size});
});

前端:

yourHttpClient.post(yourCloudFunctionUrl).toPromise().then(snap => {
     size = snap.length // will return the collection size
})


大收藏集(超过1000个文档)

最具可扩展性的解决方案


Large collection (1000+ documents)

Most scalable solution

FieldValue.increment()

FieldValue.increment()

截止到2019年4月Firestore允许完全自动地增加计数器,并且无需事先读取数据.这样可确保即使从多个源同时进行更新(以前使用事务解决),我们也具有正确的计数器值,同时还减少了数据库数量读取我们执行的内容.

As of April 2019 Firestore now allows incrementing counters, completely atomically, and without reading the data prior. This ensures we have correct counter values even when updating from multiple sources simultaneously (previously solved using transactions), while also reducing the number of database reads we perform.

通过侦听任何文档的删除或创建,我们可以添加到数据库中的计数字段中或从中删除.

By listening to any document deletes or creates we can add to or remove from a count field that is sitting in the database.

查看firestore文档-分布式计数器 或看看Jeff Delaney的数据聚合.对于使用AngularFire的任何人来说,他的指南确实很棒,但是他的课程也应该推广到其他框架.

See the firestore docs - Distributed Counters Or have a look at Data Aggregation by Jeff Delaney. His guides are truly fantastic for anyone using AngularFire but his lessons should carry over to other frameworks as well.

云功能:

export const documentWriteListener = 
    functions.firestore.document('collection/{documentUid}')
    .onWrite((change, context) => {

    if (!change.before.exists) {
        // New document Created : add one to count

        db.doc(docRef).update({numberOfDocs: FieldValue.increment(1)});
    
    } else if (change.before.exists && change.after.exists) {
        // Updating existing document : Do nothing

    } else if (!change.after.exists) {
        // Deleting document : subtract one from count

        db.doc(docRef).update({numberOfDocs: FieldValue.increment(-1)});

    }

return;
});

现在在前端,您只需查询numberOfDocs字段即可获取集合的大小.

Now on the frontend you can just query this numberOfDocs field to get the size of the collection.

这篇关于Cloud Firestore集合计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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