简体   繁体   English

如何通过Firebase Cloud函数将数据写入/更新到多个子节点?

[英]How do I write/update data to multiple child nodes via Firebase Cloud functions?

I've got a database that looks like this: 我有一个看起来像这样的数据库:

在此处输入图片说明

I want to reset the Patient_List and Current_Token children to 0 at a particular time (say 12 AM) automatically for all the users in the database. 我想在特定时间(例如12 AM)为数据库中的所有用户自动将Patient_ListCurrent_Token子级重置为0。

I went through the Firebase cloud function docs and these projects on gitHub: Deleting nodes and Delete unused user accounts using Cron . 我在gitHub上浏览了Firebase云功能文档和这些项目: 使用Cron 删除节点删除未使用的用户帐户 Both of the above examples update and change data based on a particular UID ie update only particular nodes based on the UID passed. 以上两个示例均基于特定的UID更新和更改数据,即基于传递的UID仅更新特定的节点。 I would like to know if it is possible to update values for the above child elements in all the nodes and if so , how would it be done? 我想知道是否可以在所有节点中更新上述子元素的值,如果可以,将如何完成?

It's not possible to have a single update statement affect a child of all database nodes under some location. 一个更新语句不可能影响某个位置下所有数据库节点的子级。 You will have to iterate all the nodes (by querying/reading them somehow), then update each one that you find. 您将必须迭代所有节点(通过某种方式查询/读取),然后更新找到的每个节点。 This is potentially a costly operation in terns of bandwidth. 这可能是花费大量带宽的昂贵操作。

Only use the general ref, and before that reference all nodes that you want to update, just like this: 仅使用常规引用,并在引用之前要更新的所有节点,如下所示:

var gralRef = firebase.database().ref();
var obj_update={};                              
obj_update[`users/${firstuser}/name`]='John';
obj_update[`users/${seconduser}/name`]='Mark';
obj_update[`users/${seconduser}/address`]='New York';   


gralRef.update(obj_update);                     

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase云功能-更新节点 - Firebase cloud functions - Update nodes 我如何编写 Firebase 云函数来根据另一个集合中的数据更新集合。? - how do i write a Firebase cloud function to update a collection based on the data in another collection.? 如何在Firebase Cloud Functions中删除包含嵌套子节点的子节点? - How to delete a child node including nested child nodes in Firebase Cloud Functions? 如何根据子 Cloud 函数删除 Firebase 中的某个父节点 - How do I remove a certain parent node in Firebase based on a child Cloud functions 如何在使用 Cloud Functions 时使用 Firebase Admin SDK 更改 Firebase 实时数据库中的数据? - How do I use Firebase Admin SDK to change data in Firebase Realtime Database while using Cloud Functions? 我可以通过 firebase 云功能管理员 SDK 访问 twitter 身份验证数据吗? 如果是这样,怎么做? - Can I access twitter auth data via firebase cloud functions Admin SDK? If so, how? 如何使用 Cloud Functions 计划对我的 Firebase Firestore 数据库中的集合进行批量更新? - How do I schedule a batch update to a collection in my Firebase Firestore database using Cloud Functions? 如何使用 Cloud Functions 获取数据 - How do I get data with Cloud Functions 如何在Firebase云功能中设置节点选项? - How do I set node options in firebase cloud functions? 如何在 Cloud Functions for Firebase 中获取服务器时间戳? - How do I get the server timestamp in Cloud Functions for Firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM