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": [],
      },
    );
  }

3 Answers 3

4

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")
Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

Thank you! In fact of pricing, does this variant have any advantages / disadvantages compared to the other variant from @Dharmaraj?
No, it doesn't. Billing works in the same way for both solutions.
Alright, would the $uid from the users collection be the email adress from the user? Or what exactly is the $uid?
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.
There aren't any major pros and cons but as Alex has mentioned in his answer, "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.
|
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

3 Comments

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?
user_id is not a field, instead its name of your document in collection "plans", I added image in the answer.
I wanted that every user can have multiple plans and every plan has his own document, is this possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.