检查字段是否存在-通过Firestore数据库搜索 [英] Check if a field exists - search through Firestore database

查看:59
本文介绍了检查字段是否存在-通过Firestore数据库搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个学校项目-android应用程序,该应用程序在检查Firestore的其他数据库中是否存在相应的卡号和电话号码后,将用户注册到实时数据库.目前,它仅验证第一个文档,但是如果我在其他文档中搜索这些字段,它将找不到该字段.

I am doing a project for school - android app which registers users to realtime database after it checks if there's a corresponding card number and phone number in a different database in Firestore. At the moment it verifies only the first document, but it wouldn't find the fields if I search for them in other documents.

这是我使用的方法:

    public void checkIfCardExists() {
    Query query = cardInfo.whereEqualTo("CardNo", cardNumber)
            .whereEqualTo("Phone", userPhone);

    query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    boolean documentExists;

                    if (task.isSuccessful()) {
                            Log.d("QueryResult", "Is query result empty: " + task.getResult().isEmpty());
                            documentExists = !task.getResult().isEmpty();

                    }else {
                        Log.e("QueryResult", "Error getting documents.", task.getException());
                        documentExists = false;
                    }

                    if(documentExists) {
                        Log.d("QueryResult", "The document exists");
                        Toast.makeText(com.example.transportticket.RegistrationLeap.this, "Card number found",
                                Toast.LENGTH_SHORT).show();
                        userLeap = new UserLeap(userEmail, userPass, userName, userSurname, cardNumber, userPhone);
                        registerUserLeap(userEmail, userPass);
                        startActivity(new Intent(RegistrationLeap.this, Empty.class));


                    }else{
                        Log.d("QueryResult", "The document doesn't exist or there was an error retrieving it");
                        Toast.makeText(com.example.transportticket.RegistrationLeap.this, "Card number not found",
                                Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(RegistrationLeap.this, Empty.class));
                    }
                }
            });
}

这就是我的Firestore数据库的外观 Firestore数据库

And this is how my Firestore database looks like Firestore database

我添加了一张照片,以澄清有关查找第一个文档的信息

推荐答案

如果使用的是以下代码行:

If you are using the following line of code:

Query query = cardInfo.whereEqualTo("CardNo", cardNumber)
        .whereEqualTo("Phone", userPhone);

这意味着您要告诉Firestore返回所有其中 CardNo 属性包含 cardNumber AND Phone值的文档属性保存 userPhone 的值.因此,如果您的集合中只有一个文档满足此约束,则将返回一个文档.结果中将不存在其他文档.您现在正在做的事情称为过滤.但是,如果要获取所有文档,则应删除两个 whereEqualTo()调用,或直接使用 cardInfo (这是 CollectionReference 对象).这样,您就不会过滤任何内容. CollectionReference 对象基本上是,无需任何过滤器.

It means that you are telling Firestore to return all documents where the CardNo property holds the value of cardNumber AND the Phone property holds the value of userPhone. So if in your collection only one document satisfies this constraint, a single document will be returned. The other documents won't exist in the results. What you are doing now, is called filtering. However, if you want to get all documents, then you should remove both whereEqualTo() calls or directly use cardInfo which is a CollectionReference object. In this way, you aren't filtering anything. A CollectionReference object is basically a Query without any filters.

因此,使用最后一种解决方案,您可以获得所有文档,还可以在客户端上创建过滤.不建议使用此方法,因为获取所有文档会非常昂贵.例如,如果您的收藏夹中有1000个文档,则需要支付1000次读取操作才能拥有它们.因此,由您决定哪个更适合您.

So using the last solution you can get all documents and you can also create the filtering on the client. This is not a recommended approach because getting all documents, will be very costly. For instance, if you have in your collection 1000 documents, you'll pay 1000 read operations to have them. So it's up to you to decide which one is better for you.

这篇关于检查字段是否存在-通过Firestore数据库搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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