如何从集合中的文档获取所有ID并仅更新一个布尔字段?

时间:2019-12-18 09:31:46

标签: android google-cloud-firestore

我正在使用Android Studio,我想从特定查询中更新CloudFirestore中的布尔值,我只想更新集合中具有true的文档。再次将它们设为假。

到现在为止我有了这个,我不知道为什么不起作用。有人可以帮忙吗?谢谢!

mAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
FirebaseFirestoreSettings settings = new FirebaseFirestoreSettings.Builder()
        .setTimestampsInSnapshotsEnabled(true)
        .build();
fStore.setFirestoreSettings(settings);

userID = mAuth.getCurrentUser().getUid();
fStore.collection("users/" + userID + "/Exercicis" ).whereEqualTo("fet", true)
                    .get()
                    .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                        @Override
                        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                            String data = "";

                            for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                                Exercicis exercicis = documentSnapshot.toObject(Exercicis.class);
                                exercicis.setDocumentid(documentSnapshot.getId());

                                String documentId = exercicis.getDocumentid();

                                DocumentReference exercicisusuari = fStore.document("users/" + userID + "/Exercicis/" + documentId);
                                exercicisusuari.update("fet", false);
                            }

                        }
                    });
        }

我添加了Exercicis类和数据库的结构。

enter image description here

public class Exercicis {

    private String documentid, nom, codi, info, temps,fotoperfil, fotoexercici;
    private boolean fet;

    public String getDocumentid() {
        return documentid;
    }

    public void setDocumentid(String documentid) {
        this.documentid = documentid;
    }

    public Exercicis() {
    }

    public Exercicis(String nom, String codi, String info, String temps, Boolean fet, String fotoperfil, String fotoexercici) {
        this.nom = nom;
        this.codi = codi;
        this.info = info;
        this.temps = temps;
        this.fet = fet;
        this.fotoperfil = fotoperfil;
        this.fotoexercici = fotoexercici;

    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getCodi() {
        return codi;
    }

    public void setCodi(String codi) {
        this.codi = codi;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String getTemps() {
        return temps;
    }

    public void setTemps(String temps) {
        this.temps = temps;
    }

    public boolean getFet() {return fet; }

    public void setFet(boolean fet) {this.fet = fet;}

    public String getFotoperfil() {return fotoperfil;}

    public void setFotoperfil(String fotoperfil) {this.fotoperfil = fotoperfil;}

    public String getFotoexercici() {return fotoexercici;}

    public void setFotoexercici(String fotoexercici) {this.fotoexercici = fotoexercici;}
}

0 个答案:

没有答案