簡體   English   中英

Firestore 按對象鍵查詢

[英]Firestore query by Object Key

我在 firestore 的集合中有一系列嵌套對象,它們有一個 UID 的鍵。 我想通過檢查他們的 UID 是否是對象鍵(基本上如果它存在)來查詢與用戶相關的所有文檔。 這是firestore中的模型: 在此處輸入圖片說明

這就是我目前嘗試返回文件的方式:

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

但這沒有任何回報。 我知道我可以通過該狀態進行查詢,但是它會不斷更新,因此會導致問題

以下應該可以解決問題:

  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

實際上,您可以做的是按對象中的任何字符串字段進行過濾,如下所示:

.collection("collectionName")
.where("mapArray.map.stringField", ">", "''")

這里的重要部分是字符串字段; ">", "''"

我相信無法查詢對象是否存在。 我認為您查詢對象內的字段的想法可能是最好的選擇。

您可以嘗試查詢 date_modified 字段以查找大於 1900 的任何內容。

getAllTenancyOffersByUserId() {
  return this.afs.collection('collection_name', ref =>
    ref.where(`
      application.members.${this._auth.currentUserId}.date_modified`, 
      '>=',
      new Date('1900-01-01')
    ),
  );
}

為什么不將 id 添加到對象中? 例如

    members: {
      currentMemberId: {
      date_modified:"",
      deposit_type:"",
      status:"pending",
      memberId: "currentMembderId" 
     }
    }

你做你的查詢: .collection('members').where('memberId', "==", currentMemberId)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM