1

Each user in my firestore database has a bunch of contacts. I show each user their contacts with the following query.

firebase
            .firestore()
            .collection('users')
            .doc(userId)
            .collection('contacts')
            .orderBy('lastContacted')

Now I want to introduce a new feature that lets people archive contacts.

The simplest way to do this would be to add a "status" key to the contacts and set it to "active" or "archived".

If I do this, is there any way to query for all the contacts that don't have the "status" key?

I thought about assigning the status key a value of 1 or 0 instead. I thought this would let me try the following:

firebase
            .firestore()
            .collection('users')
            .doc(userId)
            .collection('contacts')
            .where("status", "<", "1")
            .orderBy('lastContacted')

...but that does not work :(

Can I run a firestore query on a key that doesn't exist?

I want to return all contacts that don't yet have a status key.

It's an existing database so I don't want to go back and add the status field to every single contact in the database. Is there any way to avoid doing this?

I realise I can pull all contacts and handle the filtering on the client. I would prefer not to do this, this question is specific to fetching the data from firestore.

Josh Pittman
  • 7,024
  • 7
  • 38
  • 66
  • 1
    No, you can't. Firestore queries can only work against existing fields and values. – Doug Stevenson Feb 25 '20 at 07:28
  • Is there a recommended way to dealing with my predicament then? I'm assuming it common that people need/want to change the schema after they create a type of entity that is used throughout an app. – Josh Pittman Feb 25 '20 at 07:31
  • Read the duplicate for details. You need to create some data to query as your requirements demand. – Doug Stevenson Feb 25 '20 at 07:34
  • I did. The only solution was to create a null field. But this is before the fact, my question is explicitly asks what can be done after the fact. – Josh Pittman Feb 25 '20 at 07:40
  • You don't have any options if you don't have field data to query. That's just how Firestore works in order to maintain queries at massive scale. – Doug Stevenson Feb 25 '20 at 07:41
  • I understand this, thank you, what I am asking is if there is a recommended way to deal with this predicament. I am assuming it is a fairly common predicament. I have listed a) manually adding the field to all docs and b) client-side filtering as ways forward. Are there others? Any one in particular that you recommend? – Josh Pittman Feb 25 '20 at 07:44
  • As I said, with Firestore, you have no options if you're not willing to create field data that can be queried to meet your needs. – Doug Stevenson Feb 25 '20 at 07:45
  • I will point out that there are other databases that will gladly do inefficient "table scans" to search all items for missing data, if that's what you want. But those queries will not scale massively, as Firestore demands. – Doug Stevenson Feb 25 '20 at 07:47
  • Thank you Doug. I have no intention of switching. I realise there is no way to query the data with the firebase api, but there are another way to get the result. I have already listed two. I raised this question to see if people had other approaches. – Josh Pittman Feb 25 '20 at 07:50
  • You have client side filtering as your choice, which will be costly, as you have to read all documents on the client in order to perform the filtering. – Doug Stevenson Feb 25 '20 at 07:51
  • Agreed, but it's cheaper than updating every single doc in the database with the new value :( Hence, I raised the question. – Josh Pittman Feb 25 '20 at 07:52

0 Answers0