How to remove values in firebase

后端 未结 3 2030
情深已故
情深已故 2021-01-14 06:45

Just a little question :
Now i have this structure

images
---- uniqueId
-------- id_logement : 1747657
-------- image : dataimage
---- uniqueId         


        
相关标签:
3条回答
  • 2021-01-14 07:46

    Try this code:-

     rootRef.child("images").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                for(DataSnapshot dataSnapshot :  snapshot.getChildren())
                {
                   if(dataSnapshot.child("id_logement").getValue().toString().equals("1747657"))
                   {
                       dataSnapshot.getRef().setValue(null);
                   }
                }           
            }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        });
    
    0 讨论(0)
  • 2021-01-14 07:50

    There is actually an easier way.

    Just call the ref property in your snapshot, and use .on('child_added',...)

    var ref = firebase.database().ref('images');
    ref.orderByChild('id_logement').equalTo(key).on('child_added', (snapshot) => {
         snapshot.ref.remove()
    });
    
    0 讨论(0)
  • 2021-01-14 07:51

    Since you want to bulk delete data based on a query, you will need to retrieve it first and delete it setting its values to null and committing the changes with update.

    const ref = firebase.database().ref('images');
    ref.orderByChild('id_logement').equalTo(key).once('value', snapshot => {
         const updates = {};
         snapshot.forEach(child => updates[child.key] = null);
         ref.update(updates);
    });
    

    Working jsFiddle.

    0 讨论(0)
提交回复
热议问题