iosswiftfirebasegoogle-cloud-platformgoogle-cloud-firestore

Firebase read count for queries


I'm coding for iOS, using Swift 5. I'd been reading through Firebase docs and understood that Firebase works by counting reads for each document it accessed, regardless of the field data within.

So right now, I have a Firestore setup as below:

[Collection] username

  • [Document] johndoe
    • [Field] name: John Doe
    • [Field] age: 21
    • [Field] gender: Male
  • [Document] janesmith
    • [Field] name: Jane Smith
    • [Field] age: 25
    • [Field] gender: Female
  • [Document] johnnybloggs
    • [Field] name: Johnny Bloggs
    • [Field] age: 30
    • [Field] gender: Male

I have 2 questions:

Q1:

Firestore.firestore().collection("username").whereField("name", isEqualTo: "Spider Man").getDocuments()

I search through all usernames with getDocuments(), and try to match the "name". In the example above, the name is not existent, hence I shall be expecting "No username found" error. The question is, since it needs to access all the docs to actually find out which docs have the matching name, will this count as 3 reads then? Or 0 read?

Q2:

Firestore.firestore().collection("username").document("spiderman").getDocument()

I search through the docs with a specific username given. Same though, I'm expecting "No username found" error. Will this be counted as 0?

Thanks in advance!

I tried everything above, but I can't see any reads firing, So I'm not really sure how to track this myself.


Solution

  • Q1

    You'll always have to pay a number of read operations that is equal to the number of documents that are returned by a query. However, if your query yields no result, according to the official documentation regarding Firestore pricing, it said:

    Minimum charge for queries

    There is a minimum charge of one document read for each query that you perform, even if the query returns no results.

    So if, for example, you try to query 1.000 users and you get no results, you're still charged with one read. Please also note, that there is no "No username found" error. Firestore doesn't throw an Exception if the query doesn't return any documents.

    Q2

    Calling getDocument() to read a document costs one read operation in all situations.