【问题标题】:Filter Firebase collections on arrays or objects in Firebase Firestore在 Firebase Firestore 中过滤数组或对象上的 Firebase 集合
【发布时间】:2019-01-27 23:00:12
【问题描述】:

几天以来,我一直在寻找解决问题的方法。我尝试做的是从 Firestore 加载对象,并在结构中使用较低的过滤器。

/meetings (collection)
    /meetingId
        /people (object)
            - uid1:1
            - uid2:1

如果没有 where 查询,它可以正常工作。但是因为我想验证对象中的人。我靠墙走路。

FirebaseUser user = await FirebaseAuth.instance.currentUser();
    Firestore.instance
        .collection('meetings')
        .where('people.${user.uid}', isEqualTo: '1')
        .orderBy('sessionStart', descending: true)
        .snapshots()
        .listen((data) {print(data);});

返回零个结果。另外,当我自己实现array-contains 时。它不起作用。因为我想在我的数据结构上实施规则,所以我需要一个过滤。

每次执行查询时,我都会收到创建新索引的请求。 com.google.firebase.firestore.FirebaseFirestoreException: FAILED_PRECONDITION: The query requires an index. You can create it here.

当我关注 url 时,我得到创建以下索引的建议:

Collection: meetings
Fields: people.QkrzIBRs3TNHcTbYGSHsZjFPvlj2 ASC sessionStart DESC

什么会导致每个用户的索引,这不是我的愿望。

有人可以帮我解决这个问题,指导我找到正确的解决方案吗?我会很感激的!

【问题讨论】:

    标签: firebase firebase-realtime-database flutter google-cloud-firestore


    【解决方案1】:

    经过多次反复试验,我终于找到了可能的解决方案。

    sortBy 使查询无法执行。如果我删除它并且只使用where 它就完美了。

    这是解决方案:

    FirebaseUser user = await FirebaseAuth.instance.currentUser();
        Firestore.instance
            .collection('meetings')
            .where('people.${user.uid}', isEqualTo: '1')
            .snapshots()
            .listen((data) {print(data);});
    

    我需要创建的规则是:

    match /meetings/{document=**} {
        allow read, list: if resource.data.people[request.auth.uid];
    }
    

    最后我仍然缺少排序功能。所以我在创建会议对象后在代码中解决了:

    meetings.sort((Meeting from, Meeting to) => from.sessionStart
                .toIso8601String()
                .compareTo(to.sessionStart.toIso8601String()));
    

    可能有更好的解决方案,所以如果您知道更好的解决方案,请回复。

    希望这对其他人有所帮助

    【讨论】:

      猜你喜欢
      • 2019-06-07
      • 2021-11-14
      • 2020-08-19
      • 2021-05-06
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 2021-12-07
      • 2021-09-05
      相关资源
      最近更新 更多