[英]Querying Firestore map fields in flutter
I am trying to retrieve posts in my Firestore database from a field called "bookmarks"
.我正在尝试从名为"bookmarks"
的字段中检索 Firestore 数据库中的帖子。 The field "bookmarks"
is of type map. "bookmarks"
字段的类型为 map。 When a user bookmarks a post, the field is updated with the deviceId
being set to true
like this:当用户为帖子添加书签时,该字段会更新,并将deviceId
设置为true
,如下所示:
▼ bookmarks
deviceId : true
When the post is unbookmarked, deviceId: true
is deleted and the bookmarks
field remains empty like this:当帖子未添加书签时, deviceId: true
被删除,并且bookmarks
字段保持为空,如下所示:
bookmarks: {}
I am trying to retrieve posts from "bookmarks"
where deviceID
is set to true
.我正在尝试从deviceID
设置为true
的"bookmarks"
中检索帖子。 My query however returns all posts including posts where bookmarks
is empty.但是,我的查询返回所有帖子,包括bookmarks
为空的帖子。
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.谢谢你。
Your bookmarks
field is not an array, so you can't query it like an array.您的bookmarks
字段不是数组,因此您不能像数组一样查询它。 It's a map, and you can query for the values of nested fields of a map using dot notation:它是 map,您可以使用点表示法查询 map 的嵌套字段的值:
.where('bookmarks.${deviceId}', isEqualTo: true)
I tried that notation but didn't work for me, here is the notation I used: where("bookmarks.deviceId:true"), you can replace true with the value you are looking.我尝试了该表示法,但对我不起作用,这是我使用的表示法: where("bookmarks.deviceId:true"), 您可以将 true 替换为您正在查找的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.