Firestore从单个文档中检索值,但速度更快

时间:2020-06-18 16:58:37

标签: angular typescript firebase google-cloud-firestore

我想知道是否有一种方法可以从单个文档中检索值,而不必像搜索功能一样遍历每个文档。

我认为一旦数据库开始变大,不断循环遍历所有内容将变得越来越慢。

export class ProfileComponent implements OnInit {

 // Variables //
 routeParams;
 userCollection: AngularFirestoreCollection<users>;
 user: any;
 profilePicture: any = "../../assets/default-icon.png" 

 constructor(
  private afStorage: AngularFireStorage,
  public afs: AngularFirestore, 
  public afAuth: AngularFireAuth, 
  public pickerCtrl: PickerController,
  public alertController: AlertController,
  private cd: ChangeDetectorRef,
  private route: ActivatedRoute,
  private router: Router,
 ) { }

 ngOnInit() {
    this.routeParams = this.route.params.subscribe(
       params => {
        /*Update Variables here*/
        this.userCollection = this.afs.collection("users");
        console.log("collection" + this.userCollection);
        this.user = this.userCollection.snapshotChanges().pipe(map(actions => {
          return actions.map(a => {

          const data = a.payload.doc.data() as users;
          const id = a.payload.doc.id;
          if (data.email == email.__email){
            this.profilePicture = data.photoURL;
            this.cd.detectChanges();
          }

          return { id, data};
        });
      }));
  });
 }
}

执行snapshotChanges().pipe(map(actions => { return actions.map(a => {使得此循环遍历所有文档。

我仍然可以通过If Check获得结果,但是数据库越大,花费的时间越长。

想知道我是否可以基于email

搜索文档

0 个答案:

没有答案