[英]Firestore: How to store data back again in the right documents after fetching data from multiple documents
在我的應用程序中,用戶可以跟蹤他的鍛煉情況,我想將其保存在 Cloud Firestore 中。 我的想法是存儲每個月的鍛煉列表,以防止文檔變得太大。 所以文檔看起來像這樣:
month: '2022-02',
workouts: [
{
date: '2022-02-01',
exercises: [
{
sets: [{'reps': 12, 'weight': 80}
{'reps': 12, 'weight': 80}
],
},
],
},
{
date: '2022-02-02',
exercises: [
{
sets: [{'reps': 10, 'weight': 90}
{'reps': 10, 'weight': 90}
],
},
],
},
],
問題:
例如,當我獲取過去三個月的鍛煉並將它們放入列表中以讓用戶與這些鍛煉進行交互時。 如何將它們再次存儲在正確月份排序的 Cloud Firestore 中?
為了能夠將數據寫回您從中讀取的同一個文檔,除了您從文檔本身讀取的字段之外,您還需要跟蹤列表視圖中每條數據的文檔 ID。
當您擁有文檔 ID 時,向其寫入數據就像以下一樣簡單:
FirebaseFirestore.instance
.collection("yourWorkoutsCollection")
.document(documentIdForTheMonth)
.update({ 'workouts': FieldValue.arrayUnion([theWorkoutToAdd]) });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.