How to Update One Field from All Documents Using Pojo in Firestore

How to update one field from all documents using POJO in Firestore?

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.

Query Firestore data and add one field to all the documents that match the query

Is there any better way to do this?

Yes, there is. To solve this, please use the following lines of code inside onCheckedChanged() method:

Query sfRef = firestore.collection("books").whereEqualTo(uid, uid);
sfRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<String> list = new ArrayList<>();
for (DocumentSnapshot document : task.getResult()) {
list.add(document.getId());
}

for (String id : list) {
firestore.collection("books").document(id).update("anonymous", true).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "anonymous field successfully updated!");
}
});
}
}
}
});

The result of this code would be to add the anonymous property to all your book objects and set it to true. Please note, I have used the boolean true and not the String true as you have used in your code. Is more convenient to be used in this way.

P.S. If you are using a model class for your books, please also see my answer from this post.

Firestore document object (documentSnapshot) to POJO with nested maps

As you already say voucher is a Map, so that's what you should use in your Java class too:

public class MyPOJO {

public String id;
public Map<String,Voucher> vouchers;

}

public class Voucher {

public String name, description;

}

How to create/update multiple documents at once in Firestore

From Firebase documentation :

You can also execute multiple operations as a single batch, with any combination of the set(), update(), or delete() methods. You can batch writes across multiple documents, and all operations in the batch complete atomically.

// Get a new write batch
WriteBatch batch = db.batch();

// Set the value of 'NYC'
DocumentReference nycRef = db.collection("cities").document("NYC");
batch.set(nycRef, new City());

// Update the population of 'SF'
DocumentReference sfRef = db.collection("cities").document("SF");
batch.update(sfRef, "population", 1000000L);

// Delete the city 'LA'
DocumentReference laRef = db.collection("cities").document("LA");
batch.delete(laRef);

// Commit the batch
batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});

Firestore multiple write operations

Hope it helps..



Related Topics



Leave a reply



Submit