angularFire 2 的 Firebase Firestore 集合计数 [英] Firebase firestore collection count with angularFire 2

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

问题描述

我想获取 firestore 中存在的文档总数.我不想只获取数据,我拥有 200.000 项的内部产品集合的总数是使用 Angular 4-5 实现的,而不是 angular.js

I want to get the total number of the documents that exist in firestore. I don't want to get the data only the total number of inside Products collection I have 200.000 items is that possible with Angular 4-5, not angular.js

有人可以告诉我如何实现这一目标吗??

Can someone expert tell me how I can achieve that ??

到目前为止我的代码不起作用

My code so far and is not work

  get_total_messages() {
    this.messages_collection = this.afs.collection<MessageEntity>('messages');
    return this.messages_collection.snapshotChanges();
  }

这就是我尝试获取数据的方式,但不是我想要的;

this.firebase_Service.get_total_messages().subscribe( data => {
   console.log(data);
});

推荐答案

没有 API 可以获取 Firestore 集合中文档数量的计数.这意味着获得计数的唯一方法是:

There is no API to get the count of the number of documents in a Firestore collection. This means that the only ways to get the count are:

  1. 获取所有文档并在客户端对其进行计数.
  2. 将计数存储为一个单独的属性,并在您添加/删除文档时更新它.

这两种方法在 NoSQL 数据库中都很常见,当然,随着文档数量的增加,第二种方法的效率要高得多.

Both approaches are quite common in NoSQL databases, with the second of course being a lot more efficient as the number of documents grows.

Firebase 提供了一个使用 Cloud Functions 保持计数器的示例.虽然此示例是为 Firebase 实时数据库编写的,但也可以轻松修改以在 Cloud Firestore 上运行.

Firebase provides a sample of using Cloud Functions to keep a counter. While this sample is written for the Firebase Realtime Database, it can easily be modified to work on Cloud Firestore too.

Firestore 还提供有关运行聚合查询运行分布式计数器.两者似乎都比我链接的第一个样本稍微复杂.

Firestore also provides documentation on running aggregation queries and running distributed counters. Both seem slightly more involved than the first sample I linked though.

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

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