Querying Firestore map fields in flutter

核能气质少年 提交于 2021-01-20 12:59:21

问题


I am trying to retrieve posts in my Firestore database from a field called "bookmarks". The field "bookmarks" is of type map. When a user bookmarks a post, the field is updated with the deviceId being set to true like this:

▼ bookmarks
     deviceId : true

When the post is unbookmarked, deviceId : trueis deleted and the bookmarks field remains empty like this:

bookmarks: {}

I am trying to retrieve posts from "bookmarks" where deviceID is set to true. My query however returns all posts including posts where bookmarks is empty.

This is my query :

QuerySnapshot snapshot = await postsRef
        .document(postId)
        .collection('users_posts')
        .where('bookmarks', arrayContains: deviceId)
        .orderBy('timestamp', descending: true)
        .getDocuments();

Please help figure out what I am doing wrong. Attatched is the structure of my database. Thank you.


回答1:


Your bookmarks field is not an array, so you can't query it like an array. It's a map, and you can query for the values of nested fields of a map using dot notation:

.where('bookmarks.${deviceId}', isEqualTo: true)


来源:https://stackoverflow.com/questions/63005835/querying-firestore-map-fields-in-flutter

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