Javascript 如何在Firebase中查询和聚合多个集合

Javascript 如何在Firebase中查询和聚合多个集合,javascript,angular,firebase,google-cloud-firestore,angularfire2,Javascript,Angular,Firebase,Google Cloud Firestore,Angularfire2,我需要按项目和用户列表检索月度工单集合(在该月,我可能有一些项目的OdL小时数未包含在用户列表中,用户可能有一些其他项目的OdL)。 OdL模型包含项目(projectCode)和用户(userId)的字符串。 我向服务发送时间戳格式的日期、项目代码和带有用户列表的数组 this.loadGantt(from: Timestamp, to: Timestamp, projectCode: string, users: string[]){...} 我尝试执行单个查询,一个用于项目代码,一个用于

我需要按项目和用户列表检索月度工单集合(在该月,我可能有一些项目的OdL小时数未包含在用户列表中,用户可能有一些其他项目的OdL)。 OdL模型包含项目(projectCode)和用户(userId)的字符串。 我向服务发送时间戳格式的日期、项目代码和带有用户列表的数组

this.loadGantt(from: Timestamp, to: Timestamp, projectCode: string, users: string[]){...}
我尝试执行单个查询,一个用于项目代码,一个用于单个用户。 以下是该服务的查询:

this.docs = this.afs.collection<OdlModel>('odl', ref => {
      return ref.where('projectCode', '==', project)
        .where('date', '>=', from)
        .where('date', '<=', to)
        .orderBy('date', 'asc');
    })
      .snapshotChanges().pipe(map(coll => {
        return coll.map(doc => ({ id: doc.payload.doc.id, ...doc.payload.doc.data()}));
      }));
    return this.docs;

this.docs = this.afs.collection<OdlModel>('odl', ref => {
      return ref.where('userId', '==', user)
        .where('date', '>=', from)
        .where('date', '<=', to)
        .orderBy('date', 'asc');
    })
      .snapshotChanges().pipe(map(coll => {
        return coll.map(doc => ({ id: doc.payload.doc.id, ...doc.payload.doc.data()}));
      }));
    return this.docs;

如何获取单个可观察对象中的所有数据?

下面是一个片段,演示如何将AngularFire中的两个或多个Firestore集合合并到单个可观察对象中

数据结构

const usersRef = this.afs.collection('users');

const fooPosts = usersRef.doc('userFoo').collection('posts').valueChanges();
const barPosts = usersRef.doc('userBar').collection('posts').valueChanges();
解决方案

import { combineLatest } from 'rxjs/observable/combineLatest';


const combinedList = combineLatest<any[]>(fooPosts, barPosts).pipe(
      map(arr => arr.reduce((acc, cur) => acc.concat(cur) ) ),
      )
从'rxjs/observable/combineLatest'导入{combinelateest};
const combinedList=combinelatetest也可能有助于解决您的问题


让我知道这是否有用。

下面是一个片段,演示如何将来自AngularFire的两个或多个Firestore集合合并到一个可观察对象中

数据结构

const usersRef = this.afs.collection('users');

const fooPosts = usersRef.doc('userFoo').collection('posts').valueChanges();
const barPosts = usersRef.doc('userBar').collection('posts').valueChanges();
解决方案

import { combineLatest } from 'rxjs/observable/combineLatest';


const combinedList = combineLatest<any[]>(fooPosts, barPosts).pipe(
      map(arr => arr.reduce((acc, cur) => acc.concat(cur) ) ),
      )
从'rxjs/observable/combineLatest'导入{combinelateest};
const combinedList=combinelatetest也可能有助于解决您的问题

让我知道这是否有用