按对象键的Firestore查询

问题描述 投票:1回答:3

我在firestore的集合中有一系列嵌套对象,它们具有UID的键。我想通过检查他们的UID是否是对象键(基本上如果它存在)来查询与用户相关的所有文档。这是firestore中的模型:enter image description here

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

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

但这没有任何回报。我知道我可以只查询该状态,但是它会不断更新,因此会导致问题

javascript angular firebase google-cloud-firestore
3个回答
1
投票

以下应该做的伎俩:

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

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


1
投票

我相信无法查询对象是否存在。我认为你在对象中查询字段的想法可能是最好的选择。

您可以尝试查询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')
    ),
  );
}

0
投票

实际上,你可以做的是过滤你在对象中的任何字符串字段,如下所示:

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

这里重要的部分是字符串字段; “>”,“''”

© www.soinside.com 2019 - 2024. All rights reserved.