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?