Firestore - Possible to query by array-not-contains?

There is no way to query Firestore for documents that don't have a certain field or value. It's not "very difficult", but simply not possible. To learn more on why that is, see:

  • Firestore get documents where value not in array?
  • Firestore: how to perform a query with inequality / not equals
  • The Get to know Cloud Firestore episode on how Firestore queries work.

Your workaround is possible, and technically not even very complex. The only thing to keep in mind is that you'll need to load all artists. So the performance will be linear to the number of artists you have. This may be fine for your app at the moment, but it's something to definitely do some measurements on.

Typically my workaround is to track not what releases were pushed to a user, but when the last push was sent to a user. Say that a release has a "notifications sent timestamp" and each user has a "last received notifications timestamp". By comparing the two you can query for users who haven't received a notification about a specific release yet.

The exact data model for this will depend on your exact use-case, and you might need to track multiple timestamps for each user (e.g. for each artist they follow). But I find that in general I can come up with a reasonable solution based on this model.


For Elasticsearch case you need to sync with your database and elasticsearch server. And also need to make firewall rules at your Google Cloud Platform, you need keep away the arbitrarily request to your server, since it may cause bandwith cost.


The not-in operator is now available in Firestore!

citiesRef.where('country', 'not-in', ['USA', 'Japan']);

See the docs for a full list of examples:

https://firebase.google.com/docs/firestore/query-data/queries#in_not-in_and_array-contains-any