问题
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