[英]Firestore: How to store data back again in the right documents after fetching data from multiple documents
In my App a user can track his workouts, which I want to save in cloud firestore.在我的应用程序中,用户可以跟踪他的锻炼情况,我想将其保存在 Cloud Firestore 中。 My idea is to store a list of workouts for each month to prevent that a document gets too big.
我的想法是存储每个月的锻炼列表,以防止文档变得太大。 So a document would look something like this:
所以文档看起来像这样:
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}
],
},
],
},
],
Question:问题:
When I fetch for example the workouts from the last three month and put them in a list to let the user interact with these workouts.例如,当我获取过去三个月的锻炼并将它们放入列表中以让用户与这些锻炼进行交互时。 How can I store them again in cloud firestore sorted in the right month?
如何将它们再次存储在正确月份排序的 Cloud Firestore 中?
To be able to write data back to the same document you read from, you will need to track the document ID for each piece of data in your list view, in addition to the fields you read from the document itself.为了能够将数据写回您从中读取的同一个文档,除了您从文档本身读取的字段之外,您还需要跟踪列表视图中每条数据的文档 ID。
When you have the document ID, writing data to it is as easy as:当您拥有文档 ID 时,向其写入数据就像以下一样简单:
FirebaseFirestore.instance
.collection("yourWorkoutsCollection")
.document(documentIdForTheMonth)
.update({ 'workouts': FieldValue.arrayUnion([theWorkoutToAdd]) });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.