简体   繁体   中英

Querying Firestore map fields in flutter

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: true is 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.

在此处输入图像描述

在此处输入图像描述

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM