Reputation: 1627
I have a collection that stores locations and I've implemented a search function that returns suggestions based on the query string. It's something like:
if (query.length > 2) {
performSearch(query);
}
So every time a user enters a character and there are more than 2 it triggers the function performSearch()
. Now, I've read the documentation but I'm not sure I get the explanation. So my question is:
If I have 10 documents in the "locations" collection, does that means that every time the user puts a letter and trigger the search function it reads the 10 documents or it reads them only the first time and then use the "snapshot" and it doesn't count as a read?
And the other thing I don't understand is: - If I use the where method to search by name and I have 5 results in the "locations" collection does it count like 5 or 10 reads. I mean I have 10 cities and 5 of them starts with "Mad" does it reads only the 5 or the 10 documents in the collection?
Upvotes: 0
Views: 192
Reputation: 138824
When using the following if statement:
if (query.length > 2) {
performSearch(query);
}
It means that the performSearch()
method will be triggered only if the user types more than two characters.
If I have 10 documents in the "locations" collection, does that means that every time the user puts a letter and trigger the search function it reads the 10 documents
No, the method will fire only when the user types three or more characters. So you will read only the documents that satisfy your query. According to the official documentation regarding getting data in real-time:
An initial call using the callback you provide creates a document snapshot immediately with the current contents of the single document. Then, each time the contents change, another call updates the document snapshot.
And to answer the second question:
If I use the where method to search by name and I have 5 results in the "locations" collection does it count like 5 or 10 reads. I mean I have 10 cities and 5 of them starts with "Mad" does it reads only the 5 or the 10 documents in the collection?
You'll be charged with the number of document reads which is equal with the number of documents that are returned by your query. So in your case, if you have 10 documents in your collection but your query only returns 5 of them (5 of them starts with "Mad"), then you'll be charged only with 5 read operations.
Edit:
According to your comment, if you are using the real-time feature, according to the official documentation regarding pricing:
Also, if the listener is disconnected for more than 30 minutes (for example, if the user goes offline), you will be charged for reads as if you had issued a brand-new query.
So once you get a document, that document is added to the cache. So if you read that document again, you will read it from the cache. However, if your listener is disconnected, you will be changed again.
Upvotes: 2