Firebase remove node based on child value

孤街醉人 提交于 2020-12-02 08:08:18

问题


I want to delete the entire node by query like delete * WHERE user_id = "-KTruPWrYO9WFj-TF8Ft" How can I achieve this on firebase?

-KVpQFXnzQkzzrowHxGk
  answer: "1"
  question_number: 2
  user_id: "-KTruPWrYO9WFj-TF8Ft"
-KVpQFXODhsAMJYFNjy7
  answer: "4"
  question_number: 25
  user_id: "-KTruPWrYO9WFj-TF8Ft"

回答1:


To delete all references with child having some particular value first you will need to retrieve all keys ('-KVpQFXnzQkzzrowHxGk', '-KVpQFXnzQkzzrowHxGk' in your case) with equalTo query and then delete those references with remove function.

A sample code is here.

var ref = firebase.database(); //root reference to your data
ref.orderByChild('user_id').equalTo('-KTruPWrYO9WFj-TF8Ft')
    .once('value').then(function(snapshot) {
        snapshot.forEach(function(childSnapshot) {
        //remove each child
        ref.child(childSnapshot.key).remove();
    });
});


来源:https://stackoverflow.com/questions/40441625/firebase-remove-node-based-on-child-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!