Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a specific value from Firebase Database

I already followed this. But unfortunately, when I delete a data with the SAME value as the other, it gets deleted as well. I want to be deleting a single data only.

Here is my database structure. Here is my database.

I want to be only deleting only a single value.

Query query = databaseReference.orderByChild("address").equalTo(tvPatientAddress.getText().toString());

                        query.addChildEventListener(new ChildEventListener() {
                            @Override
                            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                                dataSnapshot.getRef().setValue(null);
                            }

                            @Override
                            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                            }

                            @Override
                            public void onChildRemoved(DataSnapshot dataSnapshot) {

                            }

                            @Override
                            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });

I have this code

Any chance you guys have an idea? I think the best way to do this is by getting the ID of the patient and deleting that data, since the ID is unique. Any help appreciated!

like image 629
Kyle Anthony Dizon Avatar asked Oct 16 '17 05:10

Kyle Anthony Dizon


People also ask

How do I remove a value from Firebase realtime database?

Callbacks are removed by calling the off() method on your Firebase database reference. You can remove a single listener by passing it as a parameter to off() .

How do I delete a field in firestore?

Deleting Fields For deleting a specific field from a document, use the FieldValue. delete() method when we update a document.

How do I delete multiple data in Firebase?

In order to delete multiple entries from your database, you need to know all those locations (refernces). So with other words, in the way you add data, you should also delete it. This method atomically deletes all those entries.


1 Answers

First, get the specific value that you want to delete, then remove it.

If you do not know about the key of that object, you have to query to get the object key from DataSnapshot. This can be done by using getKey() method (ex. postsnapshot.getKey())

Or, check out my Firebase Helper Classes or Firebase Helper Class Tutorial

mdatabaseReference.child("users").orderByKey().equalTo(uid).addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {

  for (DataSnapshot postsnapshot :dataSnapshot.getChildren()) {

    String key = postsnapshot.getKey();
    dataSnapshot.getRef().removeValue();

 }
like image 178
Atif AbbAsi Avatar answered Nov 15 '22 05:11

Atif AbbAsi