Mithu
Mithu

Reputation: 655

Remove Firebase Node with Specific Child Value

I have below firebase database structure.I need to delete the whole key having specific dMoble value

enter image description here

I have tried below code but its not working

 String emailID= singleToneClass.getInstance().getData();

 mDataBase=FirebaseDatabase.getInstance();
 mReferenceBooks=mDataBase.getReference("books");                       

 mReferenceBooks.orderByChild("dMoble").equalTo(emailID)
 .addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    mReferenceBooks.child(dataSnapshot.getKey()).removeValue();
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            });

Upvotes: 0

Views: 42

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80944

Change this:

@Override 
public void onDataChange(DataSnapshot dataSnapshot) {
     mReferenceBooks.child(dataSnapshot.getKey()).removeValue();
     }

Into this:

@Override
  public void onDataChange(DataSnapshot dataSnapshot) {
  for(DataSnapshot ds : dataSnapshot.getChildren()){
mReferenceBooks.child(ds.getKey()).removeValue();
   }

Your reference is at node books, to access the key you need to iterate inside of it and then use getKey()

Upvotes: 1

Related Questions