3

I have the following problem. I try to manage a n to m structure with firebase. Therefore i made three collections. collections |-----cards |----name |----text |-----cardlist |----name |----numberOfCards |-----card_cardList |----cardId |----cardListId |----number

Now I have the id of a cardListand my goal is to get all the cards which are in this list. To get them from the card_list collection i made this collection('card_list', q => q.where('cardListId', '==', myId). After that i should somehow map the value number with the cards values which I need to get somehow.

Does anyone have a suggestion how to do this?

Edited: card_cardListis the collection which shows the n to m relation between cards and cardlist

Yingrjimsch
  • 122
  • 1
  • 11
  • What different between cardlist and card_list? Your name of the `collection` already so confused – Tony Bui Jan 10 '19 at 07:22
  • I'm sorry for that. the cardlist is a collection which contains the information of the list like what's it's name, how many cards does it contain etc. The card_list is the n to m table between a card and a cardlist whit their id and an additional property – Yingrjimsch Jan 10 '19 at 07:41
  • So you want to get all the cards which are in this list in a single go? Please responde with @AlexMamo – Alex Mamo Jan 10 '19 at 07:51
  • @AlexMamo yes thats the plan. I would like to get the card and the value `number` mapped. – Yingrjimsch Jan 10 '19 at 07:58

1 Answers1

5

According to the comments:

Q: So you want to get all the cards which are in this list in a single go?

A: Yes thats the plan.

You need to know that Firestore does not have any API to get documents from multiple collections in a single step. You will have to use two (or more) separate calls. In your particular case, you should create one to get the documents under cards collection, and another one to get the documents in the cardlist collection based on the relationship between them.

For more information, please also take a look at:

Community
  • 1
  • 1
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • so did I understand it right that the right and easiest way to get my goal is to denormalize the collection `card_list` as following? – Yingrjimsch Jan 10 '19 at 08:18
  • You can denormalize your data, if this is what can quickly solve this issue. It's not a bad practice, actually is a quite common practice when it comes to Firebase. If you are new to NoQSL databases, I recommend you see this video, [Denormalization is normal with the Firebase Database](https://www.youtube.com/watch?v=vKqXSZLLnHA) for a better understanding. It is for Firebase realtime database but same rules apply to Cloud Firestore. – Alex Mamo Jan 10 '19 at 08:22
  • okay great thank you very much so I'll try to denormalize it. I watched that video, and I have one last question. Doesn't it need a lot of memory when I denormalize it like that? Let's assume i have in my case four lists. Every list contains 100 cards. In the worst case these 100 cards are everywhere the same. So I made 300 extra entries. Can't that cause a performance or memory problem? – Yingrjimsch Jan 10 '19 at 08:35
  • When you denormalize data, it means that you are duplicating objects so it can be easily found when querying the database. I'm afraid that this might not create performance or memory issues but will be costly since everything in Firestore is about the [number of reads and writes](https://firebase.google.com/docs/firestore/pricing). If you are not sure about your actual database schema, I also recommend you post another question, in which you should explain your goals and maybe there is another schema that can be more apropiate for your app. – Alex Mamo Jan 10 '19 at 08:44
  • I'll definetly accept your answer. You helped me very much. Thanks. PS: Is there a documentation what counts as read in firestore? – Yingrjimsch Jan 10 '19 at 08:53
  • [Official documentation](https://firebase.google.com/docs/firestore/) is the most important source. See the informations within each section for a better understanding. – Alex Mamo Jan 10 '19 at 09:20