I’m working on cloud firestore and let’s assume this is how my documents are structured in a collection:
{
'name': 'something'
'age': 23,
}
And I’ve some 200-300 users, and I’m storing their data like this. Now let’s say that I had to add a new field favorite_color
. So I added it to all the new user accounts that are being created right now. So now some documents look like as follows:
{
'name': 'something'
'age': 23,
'favorite_color': 'red',
}
Now I wanted to fetch these documents with the following query:
Get all docs where favorite_color is empty string or if the field just doesn't exist
. I’ve no idea how to get this done. Can this even be done? Any help will be appreciated.
2
Answers
I’m not sure how would this work with query but you can try the following code:
These are two different cases.
Let’s first start with querying for all documents for which the
favorite_color
field doesn’t exist: This is actually not possible with Firestore. The query mechanism is based on indexes and it is not possible to index a field that does not exist.Then, for the other case, i.e. querying for all documents for which the
favorite_color
field is an empty string, you can use the followingQuery
: