Firebase - Targeting a specific field of a Firestore document with cloud features

When using the cloud features with the Firebase Realtime database, you can target a specific field using the cloud feature. For example, given this JSON structure, I could target when the email field user1 has changed using the cloud function ('/user/{userId}/email').onUpdate .

{ "user": { "user1": { "name": "John Doe", "phone": "555-5555", "email": " john@gmail.com " } } }

Now with Firestore it seems that I can’t target a specific field and use only the target documents. If so, then the Firestore cloud function for targeting the email field should look something like this: ('/user/{userId}').onUpdate and shoot each time a document in the user collection changes. This will launch a large number of running cloud functions. This is how Firestore works, and is there an elegant job?

+5
source share
1 answer

You're right, because of the different data models, Cloud Firestore allows you to run cloud-based functions at document-level events rather than field-level events.

One method is to store the email in a separate document (for example, in a subset called email), so updating an email is just a change that will work. This requires you to read an additional document every time you need an email address.

Another similar method is to still have it in the same document, but also write it to a subset as the second entry to run the function. Use email as the document I have, and enter a timestamp field in the document to make it easier to clear the old document (select the oldest email address to delete, maybe even in function)

+4
source

Source: https://habr.com/ru/post/1272353/


All Articles