1

In my Fitness app by now I have only one collection users where every user has his own document. And in this document is stored all of the data from one user. Now I want to create a new collection plans so that every plan has his own document too. How can I handle it that the plans are saved by the right user? Because now the plan isnt saved anymore in the user document. Or is this the false way of data modeling?

class FireBaseHandler {
  Future is_user_registered(String email) {
    return FirebaseFirestore.instance
        .collection('users')
        .where('email', isEqualTo: email)
        .get();
  }

  Future register_new_user(email, password) {
    print(email + "->" + password);
    return FirebaseFirestore.instance.collection("users").doc(email).set(
      {
        "email": email,
        "password": password,
        "token": -1,
        "plans": [],
      },
    );
  }
Dalon
  • 566
  • 8
  • 26

3 Answers3

3

You can create a sub-collection plans in every user document. The structure would look something like this:

users -> {userId} -> plans -> {planId}
(col)     (doc)      (col)     (doc)

Here every plan has a dedicated document. This will also prevent you from hitting the 1 MB max document size by not having all plans in an array in the same document. You can easily query all plans of a user by:

FirebaseFirestore.instance.collection('users').doc("userId").collection("plans")
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
2

While @Dharmaraj answer will work, please also note that this is an alternative solution for that, in which you can use a more flatten database structure. I have answered a question a few days ago called:

In your case that would be:

Firestore-root
  | 
  --- users (collection)
  |     |
  |     --- $uid (document)
  |          |
  |          --- //user details
  | 
  --- plans (collection)
        |
        --- $planId (document)
             |
             --- uid: $uid

In this way, you can also have each "Plan" as a separate document in the "plans" top-level collection.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thank you! In fact of pricing, does this variant have any advantages / disadvantages compared to the other variant from @Dharmaraj? – Dalon Aug 17 '21 at 12:36
  • No, it doesn't. Billing works in the same way for both solutions. – Alex Mamo Aug 17 '21 at 12:38
  • Alright, would the $uid from the users collection be the email adress from the user? Or what exactly is the $uid? – Dalon Aug 17 '21 at 12:41
  • The UID is the ID of the user that comes from the authentication process. You can use instead of the $uid, an $emailAddress if you want, but email addresses can be changed, the UID remains always the same. – Alex Mamo Aug 17 '21 at 12:48
  • There aren't any major pros and cons but as Alex has mentioned in his [answer](https://stackoverflow.com/questions/68662007/what-are-the-benefits-of-using-a-root-collection-in-firestore-vs-a-subcollectio/68727499#68727499), "When it comes to NoSQL databases, a recommended practice is to have the database flattened." Sub-collections in Firestore can just be used to keep data more organized i.e. you wouldn't have to use a query/filter based on userId too view a specific user's plans. Other NoSQL databases for example MongoDB technically has no sub-collections like Firestore. – Dharmaraj Aug 17 '21 at 14:04
  • Nested documents are a thing but they count towards 16 MB doc limit there. In such databases you have no other option but to use a single root level collection plans and store the userID in each document. @Dalon – Dharmaraj Aug 17 '21 at 14:05
0

My suggestion is under collection "plans", create documents with name matching user_id of a user and store his data there. It becomes easier for you to even select documents based on user_id.

enter image description here

  • So in the user collection i need to add a field `user_id` and with this i can create a connection between the collections? How to implement that in flutter? – Dalon Aug 16 '21 at 17:23
  • user_id is not a field, instead its name of your document in collection "plans", I added image in the answer. – prudhvi madasu Aug 16 '21 at 17:28
  • I wanted that every user can have multiple plans and every plan has his own document, is this possible? – Dalon Aug 16 '21 at 18:16