0

I'm looking to get all documents from Firestore where a field does not exist OR is marked false.
The practical purpose is a product database with each document as a product. Then each document may have a field to "hide" the document such as hide_product: true.

I was looking at the documentation in Firestore and I can't seem to get a case working to get all product documents EXCEPT the ones marked with a field hide_product: true.

https://firebase.google.com/docs/firestore/query-data/queries#query_operators

const productsRef = collection(firebaseStorage, firestoreDbName)
const q = query(productsRef, where("hide_product", "==", false))
const products = await getDocs(q)

I tested with 3 documents: 1 with field missing, 1 with field as true, 1 with field as false.

Firestore fields
Product marked hide == true Product marked hide == false Product with no field
Code query
where(hide, "==", true) Shows Shows No
where(hide, "==", false) No Shows No
where(hide, "!=", true) No Shows No
where(hide, "!=", false) Shows Shows No

It would seem that using the WHERE() function excludes any document with the missing fields as mentioned mentioned in the documentation.

Is there a way to get the documents where a field is missing or false?

engineer-x
  • 2,173
  • 2
  • 12
  • 25
  • 1
    It is not possible to query for fields that don't exist. The field must contain something in order for it be part of an index, which is required for all Firestore queries. (An index can't store missing data because it doesn't know what's missing, due to the fact that documents are unstructured by nature and can contain pretty much anything.) If you want to find a document using a field, give that field some value. – Doug Stevenson Feb 05 '23 at 01:31
  • Thank you Doug; your comment along with the attached "Firestore select where a field is not present [duplicate]" were helpful! – engineer-x Feb 06 '23 at 03:00

0 Answers0