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

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

问题描述

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

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

如果是这样,我该怎么做?

If so, how do I do that?

推荐答案

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

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天全站免登陆