Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test for unset values in firebase firestore

How does one query for documents where the test is for a missing key in the document data? I'd like to catch cases where the key is absent primarily, and -- if there's a way to store undefined or null in a doc (and I'm not sure there is) -- I'd like to catch those too.

Shouldn't it be one of these...

let query = db.collection('myCollection').where('someProp','==',null);
let query = db.collection('myCollection').where('someProp','==',undefined);

But in my test with these, neither of these are finding the doc with the missing key. I'm new to firestore, so maybe I've got something else wrong with the queries.

I can't find it in the docs, but maybe special object in firestore that means "not there"?

like image 425
user1272965 Avatar asked Jul 03 '26 20:07

user1272965


1 Answers

Cloud Firestore relies upon indexes to answer queries and cannot answer any query without an index.

When a field doesn't exist, there can't be index entry for it, so there isn't a way to query for it.

Workaround #1

Query for all documents in the collection and check the returned document. Note, this can get more expensive for large collections as it results in your reading all documents

Workaround #2

If it is planned, you can have a field called schema_version. Whenever you add a new field, increment the value stored in this field (say from 12 to 13). Now if you know a field didn't exist under schema_version == 13, you can simply query for every document with a version < 13.

like image 50
Dan McGrath Avatar answered Jul 06 '26 09:07

Dan McGrath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!