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

查看:58
本文介绍了使用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天全站免登陆