title: How to Implement User-Specific Favourites using Firestore?
body: <p>My project contains an array of MealPlans. Right now, a user can <em>star</em> a MealPlan and that will update the <code>isStarred</code> Bool value in the MealPlan document. However, this just updates the database that <em>every</em> user currently accesses.</p>
  <p>How can I code it so that a user has their own personal set of <em>isStarred</em> MealPlans?</p>
  <p>I'm currently using Firebase Authentication and Firestore. This is my MealPlan struct:</p>
  <pre><code>struct MealPlan {
      
      var docID:String?
      var title:String?
      var recipeSource:String?
      var estimatedTime:String?
      var coverImageView:String?
      var equipment:[String]?
      var instructions:[String]?
      var isStarred:Bool?
      
  }
  </code></pre>
  <p>My User struct:</p>
  <pre><code>struct User {
      
      var userId:String?
      var firstName:String?
      var lastName:String?
      var email:String?
      
  }
  </code></pre>
  <p>My data model:</p>
  <pre><code>class MealPlanModel {
      
      var delegate:MealPlanProtocol?
      var listener:ListenerRegistration?
      
      func getMealPlans(_ starredOnly:Bool = false) {
          
          // Detach any listener
          listener?.remove()
          
          // Get a reference to the database
          let db = Firestore.firestore()
          
          var query:Query = db.collection(&quot;mealPlans&quot;)
          
          // If filtering for starred Meal Plans, update the query
          if starredOnly {
              query = query.whereField(&quot;isStarred&quot;, isEqualTo: true)
          }
          
          self.listener = query.addSnapshotListener({ (snapshot, error) in
              
              // Check for errors
              if error == nil &amp;&amp; snapshot != nil {
                  
                  var mealPlans = [MealPlan]()
                  
                  // Parse documents into mealPlans
                  for doc in snapshot!.documents {
                      
                      let m = MealPlan(
                          docID: doc[&quot;docID&quot;] as? String,
                          title: doc[&quot;title&quot;] as! String,
                          recipeSource: doc[&quot;recipeSource&quot;] as? String,
                          estimatedTime: doc[&quot;estimatedTime&quot;] as? String,
                          coverImageView: doc[&quot;coverImageView&quot;] as? String,
                          ingredientsProduce: doc[&quot;ingredientsProduce&quot;] as? [String],
                          ingredientsProtein: doc[&quot;ingredientsProtein&quot;] as? [String],
                          ingredientsSpices: doc[&quot;ingredientsSpices&quot;] as? [String],
                          ingredientsOther: doc[&quot;ingredientsOther&quot;] as? [String],
                          equipment: doc[&quot;equipment&quot;] as? [String], instructions: doc[&quot;instructions&quot;] as? [String],
                          isStarred: doc[&quot;isStarred&quot;] as? Bool)
                      
                      mealPlans.append(m)
                      
                  }
                  
                  // Call the delegate and pass back the notes in the main thread
                  DispatchQueue.main.async {
                      self.delegate?.mealPlansRetrieved(mealPlans: mealPlans)
                  }
                  
                  
              }
              
          })
          
      }
      
      func updateStarredStatus(_ docId:String, _ isStarred:Bool) {
          
          let db = Firestore.firestore()
          
          db.collection(&quot;mealPlans&quot;).document(docId).updateData([&quot;isStarred&quot;:isStarred])
          
      }
      
  }
  </code></pre>
  <p>And the method for starring in my View Controller:</p>
  <pre><code>@IBAction func starButtonTapped(_ sender: Any) {
          
          // Toggle the star filter status
          isStarFiltered.toggle()
          
          // Run the query
          if isStarFiltered {
              model.getMealPlans(true)
          }
          else {
              model.getMealPlans()
          }
          
          // Update the starButton
          setStarFilterButton()
          
      }
  </code></pre>
  <p>Would it involve copying the docID of a starred MealPlan into a key in the Users struct? And then displaying <em>those</em> MealPlans when filtering for <em>starred</em> MealPlans?</p>
  <p>Any help/guidance is much appreciated!</p>