Flutter Firestore where clause using map

社会主义新天地 提交于 2020-07-06 12:48:26

问题


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.

Firestore Screen

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!!


回答1:


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:

  • the blog post introducing these operations
  • the documentation on updating arrays
  • the documentation on array membership queries


来源:https://stackoverflow.com/questions/54012380/flutter-firestore-where-clause-using-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!