0

I have this document on the database: enter image description here

I want to query the amount of messages documents where the ID of the user is not in the array of "read_by". I have the following code:

const q = query(messagesRef, where('read_by', 'not-in', [user.uid]))
const docs = await getDocs(q)

But this where is not working.. It still returns the 2 documents, even tho both of them have the ID of the user inside the array. The "user.uid" is correct, returns the correct id of the user.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
jhnxx
  • 93
  • 1
  • 6

1 Answers1

0

The not-in operator includes a document in its results when a single-value field does not match of the values specified in an array. It does not handle array fields.

What you are looking for would be an array-does-not-contain operator, which Firestore does not support.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hmm.. And is there a workaround I can make? – jhnxx Nov 06 '22 at 01:13
  • No great one, as the database simply can't return items that are not its index. Check the links I included for the only option, which takes adding a map field with a default (`false`) values for all values. That's not often feasible though, hence: not a great one. – Frank van Puffelen Nov 06 '22 at 01:16
  • Well, what should I do then? Stop using firebase because I can't query for documents that does not contain a value on an array? – jhnxx Nov 06 '22 at 01:20