首页 > 解决方案 > 按对象键查询 Firestore

问题描述

我在firestore的集合中有一系列嵌套对象,它们有一个UID的键。我想通过检查他们的 UID 是否是对象键(基本上是否存在)来查询与用户相关的所有文档。这是firestore中的模型: 在此处输入图像描述

这就是我目前尝试返回文件的方式:

  getAllTenancyOffersByUserId() {
    return this.afs.collection('collection_name', ref =>
      ref.where(`application.members.${this._auth.currentUserId}`, '==', !null),
    );
  }

但这什么也没返回。我知道我可以通过该状态进行查询,但是它会不断更新,因此会导致问题

标签: javascriptangularfirebasegoogle-cloud-firestore

解决方案


以下应该可以解决问题:

  getAllTenancyOffersByUserId() {
    return this.afs.collection('members', ref =>
      ref.where(firebase.firestore.FieldPath.documentId(), '==', `${this._auth.currentUserId}`)
    );
  }

请参阅相应的文档:https ://firebase.google.com/docs/reference/js/firebase.firestore.FieldPath#.documentId


推荐阅读