简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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