【问题标题】:Reduce reading documents in firebase firestore collection减少阅读 Firebase Firestore 集合中的文档
【发布时间】:2019-06-26 20:00:33
【问题描述】:

我有网络应用程序,使用 Angular 6 和 google-clod-firestore 开发,它接受客户的订单来交付书籍;目前它有两个组件

  1. CRM 组件
  2. 下单组件

当客户拨打电话时,会在CRM中弹出,并跳转到下单页面。下单成功后会再次跳转到CRM页面。

下单组件将显示所有可用的书籍,这些书籍来自名为“books”的集合,大小为 1000;

所以每次进入下单页面都会读取1000个文档,这会产生很大的成本。我的问题是如何减少这个读取。是否可以将读取操作限制为一次。?

目前我用shareReplay和singleton服务实现了一个逻辑,不知道是不是正确的实现;

这里是代码

SingeltonService.ts
 -------------------------


export class SingeltonService {
source: Observable<any>;
aa: any;
bb: any;

constructor(private afs: AngularFirestore) {
console.log('new instance created!');
this.source = this.afs.collection('test- 
collection').valueChanges().pipe(
  tap((docs) => { console.log(`Read ${docs.length} docs`); }), 
shareReplay(1));
this.aa = this.source.subscribe()
this.bb = this.source.subscribe()

  }
}


 Order-Plcaing-component.ts
 -------------------------
export class OrderPlacingCompnent implements OnInit {

constructor(private singelton: SingeltonService){}

 ngOnInit() {}
 }

【问题讨论】:

    标签: firebase google-cloud-firestore angular6 observable


    【解决方案1】:

    一种选择是生成一个包含您希望在该订单页面上显示的所有信息的文档。可能这将是每本书的 ID 和标题,但您可能会为一本书存储更多信息,但不会显示在订单页面上。由于此合成文档中的所有数据都放在一个页面上,因此它应该完全符合文档大小的 1MB 限制。

    这种方法在 NoSQL 数据库中很常见,您经常在数据更改时做额外的工作,以使后续读取更简单/更便宜。由于读取通常更常见,这使得读取更具可扩展性。

    有关这方面的更多示例,请参阅视频 How to Structure Your Data 中出色的 Getting to know Cloud Firestore 服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-21
      • 2020-10-15
      • 1970-01-01
      • 2019-11-04
      • 1970-01-01
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      相关资源
      最近更新 更多