【问题标题】:How to retrieve data from firestore by adding where condition based on a field inside an object?如何通过基于对象内的字段添加 where 条件从 firestore 检索数据?
【发布时间】:2019-05-06 09:27:37
【问题描述】:

我使用 firebase firestore 作为我的 react native 项目的后端数据库。 我需要从数据库中检索数据。如果代码如下,则效果很好

unsubscribe = firebase.firestore().collection('messages').where('user', "==", this.props.user.uid).onSnapshot(this.onCollectionUpdate)

但是,如果“用户”字段是具有 id 字段的对象,我如何检索数据?

【问题讨论】:

    标签: javascript firebase react-native google-cloud-firestore react-native-firebase


    【解决方案1】:

    如果文档是使用如下对象创建的:

      var obj = {
        user: {
          id: '234567'
        },
        otherField: 'abcd'
      };
    

    例如创建:

      docRef.set(obj);
    

    可以如下查询:

    firebase.firestore().collection('messages').where('user.id', "==", this.props.user.uid).onSnapshot(this.onCollectionUpdate)
    

    【讨论】:

      【解决方案2】:

      你可以使用 .doc('but your id here') 代替使用 .where() 所以在你的情况下,它将是

      .doc(obj.user.id).onSnap .....
      

      【讨论】:

        猜你喜欢
        • 2014-01-09
        • 2019-12-30
        • 1970-01-01
        • 1970-01-01
        • 2013-06-19
        • 2018-09-07
        • 2017-12-28
        • 2022-11-09
        • 2021-09-22
        相关资源
        最近更新 更多