Check if a field exists - search through Firestore database

倾然丶 夕夏残阳落幕 提交于 2020-06-23 14:11:21

问题


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.

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

I added a photo to clarify about finding the first document


回答1:


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. 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.

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.



来源:https://stackoverflow.com/questions/61016299/check-if-a-field-exists-search-through-firestore-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!