简体   繁体   English

检查字段是否存在 - 通过 Firestore 数据库搜索

[英]Check if a field exists - search through Firestore database

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.我正在为学校做一个项目 - android 应用程序在检查 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.目前它只验证第一个文档,但如果我在其他文档中搜索它们,它不会找到这些字段。

This is the method I use:这是我使用的方法:

    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));
                    }
                }
            });
}

And this is how my Firestore database looks like Firestore database这就是我的 Firestore 数据库看起来像Firestore数据库的样子

I added a photo to clarify about finding the first document我添加了一张照片来说明如何找到第一个文件

If you are using the following line of code:如果您使用以下代码行:

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

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 .这意味着您要告诉 Firestore 返回CardNo属性包含cardNumber值且Phone属性包含userPhone值的所有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.但是,如果要获取所有文档,则应删除whereEqualTo()调用或直接使用作为CollectionReference cardInfo的 cardInfo。 In this way, you aren't filtering anything.这样,您就不会过滤任何内容。 A CollectionReference object is basically a Query without any filters. CollectionReference object 基本上是一个没有任何过滤器的查询

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.例如,如果您的集合中有 1000 个文档,则需要支付 1000 次读取操作才能拥有它们。 So it's up to you to decide which one is better for you.因此,由您决定哪一个更适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM