简体   繁体   中英

How can I delete specific data from each user in firebase

I was keeping the scores of users in the users table. But it is no longer needed. So I want to delete the point of each user. How can I do that?

在此处输入图像描述

There is no magic here. You'll have to read all users, loop over them, and then delete their points one-by-one, or through a multi-path update.

Something like:

DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("UsersExample");
usersRef.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Map<String, Object> updates = new HashMap<>();
        for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
            updates.put(userSnapshot.getKey()+"/point", null); // Setting to null will delete that node
        }
        userRef.updateChildren(updates);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM