alltooconfusingthereforesleep alltooconfusingthereforesleep - 5 months ago 108
Android Question

Delete Node with given id but unknown node id in Firebase

I have a firebase data structure which looks like this:

|---groups
| |---group1
| | |---membernames
| | |---xy_unknownid
| | |---givenname: "userx"
| | |---tv_unknownid
| | |---givenname: "usery"


Now I want to delete the node xy_unknownid. But I only know the "givenname": userx.

I tried to delete the node with:

mFirebaseDatabaseReference
.child("groups")
.child("group1")
.child("membernames")
.child("$member")
.child("userx")
.getParent()
.setValue(null);


but this won't work because "Invalid Firebase Database path: $member. Firebase Database paths must not contain '.', '#', '$', '[', or ']'"

So my question is, how can I delete the node: "xy_unknownid" only knowing: "userx".

Thanks in advance.

Answer

Try this code and tell me if this works,

mFirebaseDatabaseReference.child("groups").child("group1").child("membernames").addListenerForSingleValueEvent(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot snapshot) {
      for(DataSnapshot dataSnapshot1:snapshot.getChildren())
      {
         if(dataSnapshot1.child("givenname").getValue().toString().equals("userx"))
         {
             dataSnapshot1.getRef().setValue(null);
             break;
         } 
      }

   }
   @Override
   public void onCancelled(FirebaseError firebaseError) {
   }
});
Comments