获取和删除Firestore子集合中的所有文档

时间:2020-05-24 06:19:36

标签: android google-cloud-firestore

我想获取和删除名为“新闻”的子集合中的所有文档,但是它不起作用,并且没有显示任何吐司消息或错误。

如果有人告诉我哪里出了问题,这将非常有帮助。非常感谢。

这是我用来获取和删除子集合中所有文档的代码:

final String userid = FirebaseAuth.getInstance.getCurrentUser().getUid();

    FirebaseFirestore. getInstance().collection("main").document(userid).collection("journal").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (final QueryDocumentSnapshot document : task.getResult()) {

 db.collection("main").document(userid).collection("journal").document(document.getId()).delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Toast.makeText(SettingsActivity.this, "Deleted: " + document.getId(), Toast.LENGTH_SHORT).show();
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Toast.makeText(SettingsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(SettingsActivity.this, "error getting: " + e.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

Firestore结构:

collection(main) ---> document(userid)---> collection(journal)---> document 1
                                                              ---> document 2
                                                              --> document 3

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试一下。

final String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();

FirebaseFirestore.getInstance().collection("main").document(userid).collection("journal").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        for (DocumentSnapshot document: queryDocumentSnapshots.getDocuments()) {
            db.collection("main").document(userid).collection("journal").document(document.getId()).delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Toast.makeText(SettingsActivity.this, "Deleted: " + document.getId(), Toast.LENGTH_SHORT).show();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(SettingsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
        }
    }
});

另外,请确保已设置数据库规则以允许删除事件