Firebase-使用云功能定位特定的Firestore文档字段 [英] Firebase - Targeting Specific Firestore Document Field with Cloud Functions

查看:81
本文介绍了Firebase-使用云功能定位特定的Firestore文档字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将Cloud Functions与Firebase Realtime Database一起使用时,您可以使用Cloud Function定位特定字段。例如,给定这种JSON结构,我可以使用(’/ user / {userId} / email’)。onUpdate 云功能来更改user1的电子邮件字段。

When using Cloud Functions with the Firebase Realtime Database you are able to target a specific field with a cloud function. For example, given this JSON structure, I could target when user1's email field changed with a ('/user/{userId}/email').onUpdate cloud function.

{
user:{
user1:{
name: John Doe,
电话: 555-5555,
电子邮件: john@gmail.com
}
}
}

现在使用Firestore似乎无法定位特定字段,而只能定位文档。如果是这种情况,则用于存储电子邮件字段的Firestore云功能必须如下所示:('/ user / {userId}')。onUpdate 并触发 user 集合中任何文档的时间。这将导致大量浪费的云功能触发。这样就是Firestore的工作方式吗?

Now with Firestore it seems that I can't target a specific field and can only target documents. If this is the case a Firestore cloud function to target the email field would have to look like this, ('/user/{userId}').onUpdate, and fire every time any document in the user collection changed. This would result in a lot of wasted cloud functions firing. Is this how Firestore works and is there an elegant work around to it?

推荐答案

您是正确的,因为数据模型不同Cloud Firestore仅允许您在文档级别的事件而非字段级别上触发Cloud Functions。

You are correct, due to the different data models Cloud Firestore only allows you to trigger Cloud Functions on document level events rather than field level.

一种方法是将电子邮件存储在单独的文档中(例如,在名为Email的子集合中) ),因此仅更新电子邮件即可触发更改。确实需要您在每次需要电子邮件时阅读额外的文档。

One method is to store email in a separate document (e.g. in a subcollection called Email), so updating email is tye only change that will fire. This does require you reading an extra document each time you need the email though.

另一种类似的方法是将其保留在同一文档中,但也将其写入子集合作为第二次写入来触发该功能。使用电子邮件作为我想要的文档,并在文档中有一个时间戳记字段,以方便清理旧文档(选择要删除的最旧的电子邮件文档,甚至在功能中也可以)

Another similar method is to still have it in the same document, but also write it into the subcollection as a second write to trigger the function. Use email as the doc I'd and have a timestamp field in the document to make it easy to clean up the old document ( select oldest email doc to delete, maybe even in the function)

这篇关于Firebase-使用云功能定位特定的Firestore文档字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆