-1

I have a firebase data structure which looks like this:

    |---employees
         |----KJSXd4ScEmJ6N4UFc5k 
             |---employeeName:"semah"
             |---employeeAge:"24"
         |----KJW3HRh5kxm_FgU9nNV
             |---employeeName:"Alex"
             |---employeeAge:"35"

Now I want to delete the node -KJSXd4ScEmJ6N4UFc5k. But I only know the "employeeName": semah. I tried to delete the node with:

mFirebaseDatabaseReference
.child("employees")
.child("employeeName")
.child("semah")
.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: "-KJSXd4ScEmJ6N4UFc5k" only knowing: "semah" or how to remove element by the generated id "-KJSXd4ScEmJ6N4UFc5k".

Thanks in advance.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Semah Mhamdi
  • 160
  • 1
  • 10

2 Answers2

2

Since you don't have the reference to the key KJSXd4ScEmJ6N4UFc5k, you need to retrieve the value and try deleting it with the ref of value retrieved. Something like this

 database.getReference("employees").orderByChild("employeeName").equalTo("semah").addListenerForSingleValueEvent(
                new ValueEventListener() {
                    @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot child: dataSnapshot.getChildren()) {
                child.getRef().setValue(null);
            }
        }


       @Override
         public void onCancelled(DatabaseError databaseError) {
              Log.w("TodoApp", "getUser:onCancelled", databaseError.toException());
         }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Shubhank
  • 21,721
  • 8
  • 65
  • 83
  • is there a method in firebae for java to get the key "KJSXd4ScEmJ6N4UFc5k" ?? – Semah Mhamdi Jun 05 '16 at 17:46
  • this method will get you that value and you can use `dataSnapshot.getKey() ` in the `onDataChange` to get the key. My code will remove the value for that ref which will be same. – Shubhank Jun 05 '16 at 17:47
  • how i can use the method removeValue () ?? – Semah Mhamdi Jun 05 '16 at 17:49
  • 2
    i have added the code that shows how it is used. what more do you need ? calling `setValue(null)` or `removeValue()` does the same thing. https://firebase.google.com/docs/database/android/save-data#delete_data – Shubhank Jun 05 '16 at 17:49
  • but there is a problem ,if in the firebase there are two employeeName who have the same value ,how to know what value to delete so in this case we should know the key generated by firebase ? – Semah Mhamdi Jun 05 '16 at 17:54
  • they must be unique element right ? some other value must be different and you should compare that. – Shubhank Jun 05 '16 at 17:59
0

In your case you can build a query to retrieve the item to delete.
Something like:

Firebase refEmployees = new Firebase("..../employees");
Query queryRef = refEmployees.orderByChild("name").equalTo("semah");


queryRef..addListenerForSingleValueEvent(
            new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
             String key=snapshot.getKey(); 
             //Remove the item
        }


       @Override
         public void onCancelled(DatabaseError databaseError) {

         }
});
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841