admin管理员组

文章数量:1340293

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"

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"
Share Improve this question edited Nov 6, 2016 at 6:15 Frank van Puffelen 600k85 gold badges890 silver badges860 bronze badges asked Nov 5, 2016 at 18:15 Edyl Jay TempladoEdyl Jay Templado 1331 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

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();
    });
});

本文标签: javascriptFirebase remove node based on child valueStack Overflow