Flutter Firestore where 子句使用 map

Kab*_*oom 5 firebase flutter google-cloud-firestore

当发布新活动时,我将一个新的发布文档添加到集合中。在此文档中,我有一个地图,用户可以在其中向事件添加确认,将其标记为 true 并添加他自己的 ID。

防火屏

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

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

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

谢谢!!

Fra*_*len 6

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

具有这样的数组的等效查询将变为:

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

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

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