Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am aware it would be very difficult to query by a value that does not exist in an array but is there a way to do this without doing exactly that?

Here is my scenario - I have a subscription based service where people can opt in and "follow" a specific artist. In my backend, this creates a subscription doc with their followerId, the id of the artist they want to follow (called artistId), and an array called pushed. The artist can add new releases, and then send each follower a notification of a specific song in the future. I would like to keep track of which follower has been pushed which release, and this done in the aforementioned pushed array. I need a way to find which followers have already been pushed a specific release and so...

I was thinking of combining two queries but I am not sure if it is possible. Something like:

  1. db.collection('subscriptions').where('artistId', '==', artistId)
  2. db.collection('subscriptions').where('artistId', '==', artistId).where('pushed', 'array-contains', releaseId)

And then take the intersection of both query results and subtract from the 1st query to get the followers that have not been pushed a specific release.

Is this possible? Or is there a better way?

like image 853
nolbuzanis Avatar asked Jan 05 '20 00:01

nolbuzanis


People also ask

Are firestore arrays ordered?

Arrays are not sorted in RTD nor Firestore objects however are sorted by it's keys.

Does firestore Create collection if not exists?

Collections and documents are created implicitly in Cloud Firestore. Simply assign data to a document within a collection. If either the collection or document does not exist, Cloud Firestore creates it.


2 Answers

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.

like image 178
Frank van Puffelen Avatar answered Nov 13 '22 09:11

Frank van Puffelen


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.

like image 39
Tahir Kizir Avatar answered Nov 13 '22 08:11

Tahir Kizir