0

I have this collections tree:

users/portfolios/rents/events

So in order to find specific events for all users I can:

  1. Collection Group Query
  2. Move events to a separate collection with userID, portfoliosID and rentsID. And in rents documents, have a list of different eventsIDs

What option is better based on Firebase's monthly limitation plan and performance?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Dani
  • 3,128
  • 2
  • 43
  • 91

1 Answers1

0

You can use a collection group query along with a "WHERE" clause. So I cannot see a reason why you would change this structure. Don't forget to create also an index.

What option is better based on Firebase's monthly limitation plan and performance?

Regarding costs, it doesn't really matter if you use a collection group query or a regular query, you'll always have to pay a number of reads that is equal to the number of documents that are returned.

Regarding performance, none. In Firestore the query performance depends on the number of documents you request and not on the number of documents you search. It doesn't really matter if you get 10 documents out of 100 or out of 100 million, the response time will always be the same.

Besides that, in the NoSQL world, we are usually structuring a database according to the queries that we want to perform. So keep in mind that.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • It's interesting that you pay per documents returned. IMO I think this forces to have unnecessary arrays or a not a really well designed DB for some cases. For example I ended up here restructuring my collections: https://stackoverflow.com/questions/73354391/firebase-operation-was-rejected-because-the-system-is-not-in-a-state-required-f/73355772?noredirect=1#comment129560564_73355772 Now, the best solution is having separated leases with rentID (and maybe userID and portfolioID). Instead of getting all the leases as I'm getting now when I get the rent I have to filter leases by rentID. – Dani Aug 15 '22 at 19:03
  • So this multiplies the number of documents returned by all my users incrementing costs. Am I correct? If so, is there any other way to reduce here costs? – Dani Aug 15 '22 at 19:05
  • Having arrays is indeed a well-designed DB. Maybe this [resource](https://medium.com/firebase-tips-tricks/how-to-reduce-firestore-costs-8cb712473e83) will help. – Alex Mamo Aug 16 '22 at 07:19
  • But how would you filter by someone's comments? Following my link it's not possible create an index on a specific item in an array field. If for example you want to take all comments that Anna made having that structure how do you do it? – Dani Aug 16 '22 at 08:05
  • If you have another specific use case, please post a new question, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Aug 16 '22 at 08:30
  • I'm talking about that example you linked. When you have something like this: https://ibb.co/tm4cqrn how do you get all comments from a specific user? In my other question I've been told that that's not possible – Dani Aug 16 '22 at 09:08
  • Most likely creating another collection or using a [collection group query](https://firebase.google.com/docs/firestore/query-data/queries#collection-group-query). But keep in mind that we are usually structuring a Firestore database according to the queries that we want to perform. – Alex Mamo Aug 16 '22 at 09:16
  • I'm a bit confused. I thought a collection group query wasn't an option here since we cannot create an index on a specific item in an array field. Otherwise, if we don't create this index we can face the "ensure it has been indexed via the Firebase console" error as I mention in that question – Dani Aug 16 '22 at 09:46
  • I was saying to use that if you refactor the database. However, if you have another specific question, post it, here on StackOverflow, using its own [MCVE](https://stackoverflow.com/help/mcve), so I and other Firebase developers can help you. – Alex Mamo Aug 16 '22 at 09:59
  • If I refactor my database I could use that, yes, but I'm talking about your article and that last image I took from it. How would you get all the comments from one user having that structure? – Dani Aug 16 '22 at 10:06
  • 1
    You cannot. You should refactor your database according to the queries you intend to perform. That structure is particular for that use case. – Alex Mamo Aug 16 '22 at 10:19
  • so that's what I meant at the beginning: despite having arrays is indeed a well-designed DB and they are handy to use, we can face situations like this, where we have to refactor a working structure. We can't know in advance all the queries we need to perform. DBs/apps grow and sometimes we need new features. Unfortunately, this situation will increase my costs, returning individual and separate leases instead of an array (which is just a single document at the end). – Dani Aug 16 '22 at 10:42