问题
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.
I went through the Firebase cloud function docs and these projects on gitHub: Deleting nodes and Delete unused user accounts using Cron. Both of the above examples update and change data based on a particular UID i.e update only particular nodes based on the UID passed. 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?
回答1:
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.
回答2:
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);
来源:https://stackoverflow.com/questions/53024212/how-do-i-write-update-data-to-multiple-child-nodes-via-firebase-cloud-functions