0

I am trying to build a stream of posts where posts hidden by current user = 'hidingUserId' are not shown in the list of posts. Is there a reverse queiry command for arrayContains? I need something like:

stream: postRef
          .where('hidingUserId', arrayDOESNOTcontain: 
            FirebaseAuth.instance.currentUser.uid)
          .orderBy('stars', descending: false)
          .orderBy('timestamp', descending: true)
          .snapshots(),`enter code here`

Or what would be other ways to easily implement it? Thank you! it is an array, as several users might hide the post, thus creates an array with userIds.

HaKim
  • 277
  • 2
  • 12
  • No, there's no such operation since queries in Firestore are based on what **is** in an index, so they can't return documents that don't exist in the index. This has been covered quite regularly before, so I recommend checking https://stackoverflow.com/search?q=%5Bgoogle-cloud-firestore%5D+array+not+contains and https://stackoverflow.com/search?q=%5Bgoogle-cloud-firestore%5D%5Bflutter%5D+array+not+contains – Frank van Puffelen Oct 29 '21 at 16:32
  • Oh, I see! Thank you very much! – HaKim Oct 30 '21 at 03:55

1 Answers1

0

No, there's no such operator.

You can use Firestore rules to make posts invisible to the user:

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to read data if the document's hidingUserId 
    // not equal to the requesting user's id
    match /posts/{post} {
      allow read: if resource.data.hidingUserId != request.auth.uid;
    }
  }
}

Then it's safe to remove where clause whatsoever:

stream: postRef
          .orderBy('stars', descending: false)
          .orderBy('timestamp', descending: true)
          .snapshots()
Andrey Gordeev
  • 30,606
  • 13
  • 135
  • 162
  • Arigato very much!!! Will try this approach tonight!! Bolishoe spasibo! – HaKim Oct 30 '21 at 03:55
  • @HaKim no problem. Please don't forget to mark my answer if you find it useful – Andrey Gordeev Oct 30 '21 at 04:10
  • I have updated my rules as per your suggestion: match /reviews/{userId=**} { allow read: if resource.data.hidingUserId != request.auth.uid; allow delete, update, create: if request.auth != null; }. however, I am getting missing or insufficiaent permission. in the rules playground: Null value error on the "resource"... pls, help? – HaKim Nov 01 '21 at 10:22