如何在Firestore上检查空文档? [英] How to check for an empty document on Firestore?

查看:57
本文介绍了如何在Firestore上检查空文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图删除Firebase Firestore上不包含任何字段的文档.但是,在删除文档之前,如何检查文档中是否没有数据?

So I am trying to delete a document on Firebase Firestore which contains no fields. But how can I check whether the document contains no data before I delete it?

推荐答案

要解决此问题,您应该以Map的形式从数据库中获取数据:

To solve this, you should get the data from the database as a Map:

yourDocumentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Map<String, Object> map = document.getData();
                if (map.size() == 0) {
                    Log.d(TAG, "Document is empty!");
                } else {
                    Log.d(TAG, "Document is not empty!");
                }
            }
        }
    }
});

要仅检查文档是否存在,并不表示该文档为空.该文档可以存在(如您的屏幕快照中所示),但没有设置属性.由于Cloud Firestore数据库中的每个文档都是Map,因此您可以使用size()方法来查看是否为空.

To check a document only for existens it doesn't mean that is empty. The document can exist (like in your screenshot) but has no properties set. Because every document in a Cloud Firestore database is a Map, you can use size() method to see if is empty or not.

这篇关于如何在Firestore上检查空文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆