Flutter Firestore where子句使用地图 [英] Flutter Firestore where clause using map

查看:92
本文介绍了Flutter Firestore where子句使用地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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上的索引,该索引就会自动添加.

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

When a new activity is posted i add a new post document into the collection. Inside this document i have a map where users add confirmation to the event marking it as true and adding his own id.

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

With this snippet i'm able to get all the posts confirmed by the user. The issue here is to get this a index is required to perform this query. And this index can't be generic. I can't create a index for each user.

Some idea of how to get it?

Thanks!!

解决方案

You'll want to turn the confirmations field into an array, and use the (relatively recent) array-contains and arrayUnion operations.

The equivalent query with an array like that would become:

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

And this way you only need an index on confirmations, which is added automatically.

For more on these see:

这篇关于Flutter Firestore where子句使用地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆