whereGreaterThanOrEqualTo and whereArrayContains number of reads in firestore

Viewed 166

It is a pretty general question but I couldn't find any document that explains this.

I use firestore as my main database for my app.

I recently implemented an autocomplete system by querying my firestore in the following 2 ways (first way to catch parts of the word and second to catch whole word if it is not at the beginning of the sentence):

db.collection( "IDS" )
        .whereGreaterThanOrEqualTo("ID", s.toString() ).whereLessThanOrEqualTo( "ID", s.toString() + "\uF7FF" ).limit(10).get()
        .addOnCompleteListener( new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        String id = document.getString( "ID" );
                        ids.add( id );
                    }

                }

            }
        } );

db.collection( "IDS" )
        .whereArrayContains("IDArray", s.toString() ).limit(10).get()
        .addOnCompleteListener( new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        List<String> idsParts = (List<String>) document.get( "IDArray" );
                        String id = TextUtils.join( " ", idsParts );
                        ids.add( id);
                    }

                }

            }
        } );

This code works well however I'm trying to understand the way it works.

Let say I have 1 million documents in my IDS collection. When I use the queries above, does it searches all of the 1 million documents and checks each one? Or more specifically, does it mean that it will count as 1 million reads or eventually since I used .limit(10) it means that only 10 reads were used?

I'm asking because this app should be published soon and I would like to figure out if the methods above will be very expensive to query.

If there is another way to optimize the query I would be happy to know.

Thank you

1 Answers

The concept is simple:

Number of documents resulted = number of read operations charged

And yes when you use limit, you tell the query to give you your-specified number of result.

For eg. If you are executing query on 100 documents with which your query satisfies only 15 documents, you will be charged for just 15 documents. And further on if you use limit(10), you will be resulted with only 10 documents (with 10 reads).

Related