简体   繁体   中英

How to search for a particular word in whole document in firestore?

I am trying to make an android app for college. I have a collection named users. All the students who create accounts are stored in this collection within a document named same as their particular UID. I have a field inside every document named admission_id which contains a unique id. Whenever a student creates an account, he/she needs to enter a unique admission_id. I need to check whether the entered admission id is unique or not.

collectionReference = (CollectionReference) fStore.collection("Users");
Query query = collectionReference
        .whereEqualTo("Admission", "1234");
query.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        Toast.makeText(CreateStudent.this, "Admission ID already exists", Toast.LENGTH_SHORT).show();
    }
});

I tried the above code but it always returns true. I want to check 1234 in all the collections "Admission" field and display toast if it exist.

The onSuccess will always be called, even when there are no results.

To check whether there are any results for the query, check queryDocumentSnapshots.isEmpty() .

query.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
        if (!queryDocumentSnapshots.isEmpty()) {
            Toast.makeText(CreateStudent.this, "Admission ID already exists", Toast.LENGTH_SHORT).show();
        }
    }
});

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