Javascript 如何实时查询firestore w.r.t?

Javascript 如何实时查询firestore w.r.t?,javascript,angular,firebase,google-cloud-firestore,angularfire2,Javascript,Angular,Firebase,Google Cloud Firestore,Angularfire2,因此,notifications集合中存储了一个通知对象列表,我的目标是检索所有早于当前日期时间的通知 所以我的基本问题是: return this.db.collection(environment.collection, ref => ref.where('staffRole', '==', staffRole).where('notificationDate', '<=', new Date().getTime()).orderBy('notificationDate'

因此,notifications集合中存储了一个通知对象列表,我的目标是检索所有早于当前日期时间的通知

所以我的基本问题是:

    return this.db.collection(environment.collection, ref => ref.where('staffRole', '==', staffRole).where('notificationDate', '<=', new Date().getTime()).orderBy('notificationDate', 'desc'))
      .snapshotChanges()
      .pipe(map(snaps => {
        return snaps.map(snap => {
          return <Notification>{
            id: snap.payload.doc.id,
            ...Object.assign({}, snap.payload.doc.data())
          }
        });
      }));
我的逻辑是每秒重新订阅可观察到的内容。它确实起了作用,但用于读取文档的数据库使用量急剧上升。所以基本上这个逻辑也不能用

有没有其他方法可以达到我想要达到的目标。我愿意接受任何建议,即使是关于更改我的界面,只要我检索有关实时的通知

接口:

export interface Notification {
    id: string,
    isRead: number,
    jobNo: number,
    notificationDate: number,
    notificationMessage: string,
    notificationTitle: string,
    notificationType: number,
    receiverId: string,
    staffRole: number
}

我将服务中的查询更改为简单查询:

        return this.db.collection(environment.collection, ref => ref.where('receiverId', '==', staffId))
          .snapshotChanges()
          .pipe(map(snaps => {
            return snaps.map(snap => {
              return <Notification>{
                id: snap.payload.doc.id,
                ...Object.assign({}, snap.payload.doc.data())
              }
            });
          }));
返回此.db.collection(environment.collection,ref=>ref.where('receiverId','==',staffId))
.snapshotChanges()
.pipe(贴图(捕捉=>{
返回snap.map(snap=>{
返回{
id:snap.payload.doc.id,
…Object.assign({},snap.payload.doc.data())
}
});
}));
在我订阅时应用了我所有的逻辑:

    this.notificationService.getNotifications(getStaffRolevalue(this.staffRole),
      (this.staffRole === 'csa' || 'ops' || 'admin' ? null : this.loggedInStaffId)).subscribe(notifications => {
        this.timer !== undefined ? this.timer.unsubscribe() : false;
        this.timer = interval(5000)
          .pipe(
            map(tick => new Date()),
            share()
          ).subscribe(date => {
            this.notifications = notifications.filter(notification => notification.notificationDate <= date.getTime()).sort(function (a, b) { return b.notificationDate - a.notificationDate });
            const x = this.notificationCount;
            this.notificationCount = this.notifications.filter(notification => notification.isRead === 0).length;
            const y = this.notificationCount;
            y - x === 1 ? this.playAudio() : false;
          });
      });
this.notificationService.getNotifications(getStaffRolevalue)(this.staffRole),
(this.staffRole=='csa'| |'ops'| | |'admin'?null:this.loggedInStaffId)).subscribe(通知=>{
this.timer!==未定义?this.timer.unsubscribe():false;
this.timer=间隔(5000)
.烟斗(
映射(勾选=>新日期()),
股份()
).订阅(日期=>{
this.notifications=notifications.filter(notification=>notification.notificationDate notification.isRead==0);
const y=this.notificationCount;
y-x==1?this.playAudio():false;
});
});
    this.notificationService.getNotifications(getStaffRolevalue(this.staffRole),
      (this.staffRole === 'csa' || 'ops' || 'admin' ? null : this.loggedInStaffId)).subscribe(notifications => {
        this.timer !== undefined ? this.timer.unsubscribe() : false;
        this.timer = interval(5000)
          .pipe(
            map(tick => new Date()),
            share()
          ).subscribe(date => {
            this.notifications = notifications.filter(notification => notification.notificationDate <= date.getTime()).sort(function (a, b) { return b.notificationDate - a.notificationDate });
            const x = this.notificationCount;
            this.notificationCount = this.notifications.filter(notification => notification.isRead === 0).length;
            const y = this.notificationCount;
            y - x === 1 ? this.playAudio() : false;
          });
      });