How to update one field from all documents using POJO in Firestore?
Solution 1:
You can solve this, in a very simple way. Beside the getter, you should also create a setter for your active
property like this:
public void setActive(boolean active) {
this.active = active;
}
Once you have created the setter, you can use it directly on your student
object like this:
db.collection("students").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (DocumentSnapshot document : task.getResult()) {
Student student = document.toObject(Student.class);
student.setActive(false); //Use the setter
String id = document.getId();
db.collection("students").document(id).set(student); //Set student object
}
}
}
});
The result of this code would be to update the active property of all you student objects to false
and set the updated object right on the corresponding reference.