1
votes

I am learning my basics for Firestore and trying to build an app which allows user1 to share a document with user2/3/4 etc.

For billing purposes, every query which results in a document read counts towards the cost. So, I do not want to follow the approach of adding the user2/3/4 etc emails to a 'sharedWith' variable to type: array or map type structure as I believe every user will then have to scan the entire collection and pick the documents where their email appears.

Is there any other approach to this where user1 can programmatically give access to user2/3/4 of one specific document?

1

1 Answers

0
votes

For billing purposes, every query which results in a document read counts towards the cost.

That's correct and according to the official documentation regarding Cloud Firestore billing:

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

So you're also charged with one document read, even if your query does not return any results.

I believe every user will then have to scan the entire collection and pick the documents where there email appears.

That's also correct. So let's assume the email address that you are looking for exist in a document that is appart of 10k collection of documents. So if you query the database only for that particular document, you'll be charged with only one document read and not for those 10k. So you are charged according to the number of items you get back and not to the number of items your request them from. And this is available for the first request, when you get the data from the Firebase servers. If in the meanwhile nothing has changed, second time you get the data from the cache since Firestore has the offline persistence enabled by default. Which means you aren't charged for any other document reads.

Is there any other approach to this where user1 can programatically give access to user2/3/4 of one specific document?

Without writing the data to database, there is not. So you should add the ids or email addresses to the desired documents and perfom a query according to it.