简体   繁体   English

Firestore:如何在从多个文档中获取数据后将数据再次存储回正确的文档中

[英]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.

相关问题 为 RecyclerView 获取多个 Firestore 文档 - Fetching Multiple Firestore Documents for RecyclerView 如何从 Cloud Firestore 中删除多个文档? - How to delete multiple documents from Cloud Firestore? 如何在 Firestore 中加入多个文档? - How to join multiple documents in a Firestore? 如何从 firebase firestore 中的多个嵌套子集合中查询多个文档? - How to query multiple documents from multiple nested subcollections in firebase firestore? 将来自 Flutter 应用程序的数据与 Firestore 文档中的数据进行比较 - Compare data from Flutter app with the data in Firestore documents 在 Javascript 中从 Firestore 获取 forEach() 之后的数据 - Fetching data after forEach() from Firestore in Javascript 获取 firestore 数据库时,“itemCount:snapshot.data.documents.length”不再有效。 有谁知道如何解决这个问题? - when fetching firestore database "itemCount: snapshot.data.documents.length" is not working anymore. does anyone know how to solve this problem? 解决从 firestore 获取数据的多个承诺 - Resolving multiple promises for fetching data from firestore 使用 Cloud firestore 触发器同步文档时如何保持数据一致? - How to keep data consistent when sync documents with cloud firestore triggers? 如何从 firestore 一次获取多个集合组的文档? - How to get multiple collection group's documents at once from firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM