Home » Android » how to search at firestore document's field

how to search at firestore document's field

Posted by: admin November 1, 2017 Leave a comment

Questions:

How to search at firestore documents? if firestore collection contains certain document and if there has a string field at document named ‘title’. How can i search specific title using firebase android api.

Answers:

It is documented in the Docs here, in the last section of the page, titled Get multiple documents from a collection.

Firestore provides a whereEqualTo function to query your data.

Example code (from Docs):

db.collection("cities")
        .whereEqualTo("capital", true) // <-- This line
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                        Log.d(TAG, document.getId() + " => " + document.getData());
                    }
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });