简体   繁体   English

当我有多个唯一标识时从 Firebase 数据库读取数据

[英]Read data from Firebase database when I have multiple unique ids

I'm having trouble writing in my database because I have multiple unique ids that I don't know how to access.我在写入数据库时遇到问题,因为我有多个不知道如何访问的唯一 ID。 I know that for writing I need to use something like this:我知道为了写作我需要使用这样的东西:

DatabaseReference ref=database.getReference().child("Children")
ChildData data = new ChildData(fullName,age);
ref.push().setValue(data);

My database looks like this:我的数据库看起来像这样:

  {
  "Children" : {
    "55hxObZjRLY9PSZxZxGgSTRKzpc2" : {
      "-MzUih5e40OsWPF_Dj0q" : {
        "age" : "22",
        "fullName" : "Cristina "
      },
      "plays" : {
        "-MznnNih5fItYf2usXB4" : {
          "centiseconds" : "70",
          "currentDate" : "01.04.2022",
          "currentHour" : "04:23:30",
          "numberOfFails" : "5",
          "seconds" : "2"
        }
      }
    }
  },
  "Data" : {
    "id1" : {
      "centiseconds" : "70",
      "currentDate" : "01.04.2022",
      "currentHour" : "04:23:30",
      "numberOfFails" : "5",
      "seconds" : "2"
    }
  }
}

I need the "plays" child to be under "fullName" in "-MzUih5e40OsWPF_Dj0q", not as a separated child.我需要“播放”孩子在“-MzUih5e40OsWPF_Dj0q”中的“全名”下,而不是作为一个分离的孩子。 How can I access the path without hardcoding it like this ref.child("MzUih5e40OsWPF_Dj0q").child("plays").push().setValue(data) ?如何在不像这样硬编码的情况下访问路径ref.child("MzUih5e40OsWPF_Dj0q").child("plays").push().setValue(data) I will have multiple children in "Children" with different ids, I won't be able to harcode them.我将在“孩子”中有多个具有不同 ID 的孩子,我将无法对它们进行硬编码。

Is there any function that returns the path for the unique id?是否有任何 function 返回唯一 ID 的路径?

Here is the entire function:这是整个 function:

public void writeNewData
            (String fullName,String age) {
    DatabaseReference reference = database.getReference().child("Data");         
    DatabaseReference ref=myRef.push();
    ChildData data = new ChildData(fullName,age);
    ref.push().setValue(data);
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot ds : snapshot.getChildren()) {
                DatabaseReference playRef=ref.child("plays");
                DatabaseData databaseData = ds.getValue(DatabaseData.class);
                playRef.push().setValue(databaseData);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
}

Edit: This is my desired schema:编辑:这是我想要的架构:

{
  "Children" : {
    "55hxObZjRLY9PSZxZxGgSTRKzpc2" : {
      "-MzUih5e40OsWPF_Dj0q" : {
        "age" : "22",
        "fullName" : "Cristina ",
        "plays" : {
          "-MznnNih5fItYf2usXB4" : {
            "centiseconds" : "70",
            "currentDate" : "01.04.2022",
            "currentHour" : "04:23:30",
            "numberOfFails" : "5",
            "seconds" : "2"
          }
        }
      },
    }
  },
  "Data" : {
    "id1" : {
      "centiseconds" : "70",
      "currentDate" : "01.04.2022",
      "currentHour" : "04:23:30",
      "numberOfFails" : "5",
      "seconds" : "2"
    }
  }
}

If you want the plays information to be a sibling of the age and fullName properties, then you will need to have a plays field/property in your ChildData class.如果您希望plays信息成为agefullName属性的兄弟,那么您需要在ChildData class 中有一个plays字段/属性。

You'll need two classes, one to represent a "play" and one to represent a "child".您将需要两个类,一个代表“游戏”,一个代表“孩子”。 At its minimum these will look like:这些至少看起来像:

public class Play {
  public String centiseconds, currentDate, currentHour, numberOfFails, seconds;
}

public class Child {
  public String age, fullName;
  public Map<String, Play> plays = new HashMap<>();
}

If you read/write this class, you will get plays nested under the child's data.如果你读/写这个 class,你会得到嵌套在孩子数据下的plays

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 firebase 实时数据库中单个孩子下的多个推送 ID 中检索数据 - Retrieve data from multiple push ids under a single child in firebase realtime database 从 javascript firebase 数据库读取数据时,如何读取嵌套的多个引用? - How can we read multiple references nested while reading data from javascript firebase database? 尝试从 Firebase 实时数据库读取数据时出现错误“Static 方法‘buildBlock’要求‘UInt’符合‘View’” - I am getting error" Static method 'buildBlock' requires that 'UInt' conform to 'View'" when trying to read data from Firebase Realtime Database 为什么无法从数据库 Firebase 读取数据? - Why Can't read data From Database Firebase? 当我从 Firebase Android Studio 读取或写入数据时,我的签名 APK 崩溃 - My Signed APK Crash when i read or write data from Firebase Android Studio 从 flutter 中的 firebase 实时数据库中读取嵌套数据 - Read nested data from firebase realtime database in flutter 如何使用 Flutter/Dart 从 Firebase 实时数据库中读取数据 - How to read data from Firebase Realtime database using Flutter/Dart 从 Firebase 的实时数据库中读取数据并将其放入数组中 - Flutter - Read data from Firebase's Realtime Database and put it in an array - Flutter 尝试从 Firebase 中读取 Firebase 数据库时无法确定 Firebase 数据库 URL Node.js Firebase function - Can't determine Firebase Database URL when trying to read Firebase Database from within Node.js Firebase function 无法从 Android Studio 中的 Firebase 数据库读取数据 - Not able to read data from Firebase Database in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM