gpt4 book ai didi

firebase - 使用 map 的 Flutter Firestore where 子句

转载 作者:IT王子 更新时间:2023-10-29 06:40:22 25 4
gpt4 key购买 nike

发布新事件时,我会在集合中添加一个新的发布文档。在本文档中,我有一张 map ,用户可以在其中向事件添加确认,将其标记为 true 并添加自己的 ID。

Firestore Screen

var snap = await Firestore.instance
.collection('user_posts')
.where("confirmations.${user.id}",isEqualTo: true)
.getDocuments();

通过此代码段,我能够获得用户确认的所有帖子。这里的问题是要获得执行此查询所需的索引。而且这个索引不能是通用的。我无法为每个用户创建索引。

关于如何获得它的一些想法?

谢谢!!

最佳答案

您需要将 confirmations 字段转换为数组,并使用(相对较新的)array-containsarrayUnion 操作.

使用类似数组的等效查询将变为:

var snap = await Firestore.instance
.collection('user_posts')
.where("confirmations", arrayContains: user.id)
.getDocuments();

这样你只需要一个关于 confirmations 的索引,它是自动添加的。

有关这些的更多信息,请参阅:

关于firebase - 使用 map 的 Flutter Firestore where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54012380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com