首页 > 解决方案 > Flutter Firestore where子句使用map

问题描述

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

Firestore 屏幕

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

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

一些想法如何得到它?

谢谢!!

标签: firebasefluttergoogle-cloud-firestore

解决方案


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

具有类似数组的等效查询将变为:

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

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

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


推荐阅读