是否可以将 pushID 绑定到 Firebase 数据库中的 userUid

Is it possible to bind a pushID to a userUid in a Firebase database

我正在尝试从我数据库的另一个分支中的用户 UID 创建 PushID。

到目前为止我已经这样做了:

DatabaseReference userUUID = FirebaseDatabase.getInstance().getReference().child(user.getUid());
        
        String pushUser = userUUID.push().getKey();
        
        HashMap<String, Object> withdrawMap = new HashMap<>();
        withdrawMap.put("id", pushUser);
        withdrawMap.put("balance", "5000");
        withdrawMap.put("email", emailMP);
        withdrawMap.put("date", dateString);
        withdrawMap.put("statut", "Pending");
        withdrawMap.put("image", mediumPackImage);
        
        referenceWithdraw.child(pushUser)
            .setValue(withdrawMap)
            .addOnCompleteListener(new OnCompleteListener<Void>() {}

这是我的 .json 格式的数据库(第一个和第二个来自 2 个不同的用户)

{
  "Withdraw" : {
    "-Mn1-FONHwx1-EmwZKND" : {
      "amount" : "5000",
      "date" : "27/10/2021",
      "email" : "example@gmail.com",
      "id" : "-Mn1-FONHwx1-EmwZKND",
      "statut" : "Pending"
    },
    "-Mn1-ZI4lkbv4P76UMQl" : {
      "amount" : "5000",
      "date" : "27/10/2021",
      "email" : "example@gmail.com",
      "id" : "-Mn1-ZI4lkbv4P76UMQl",
      "statut" : "Pending"
    }
  }
}
referenceWithdraw = FirebaseDatabase.getInstance().getReference().child("Withdraw");

我想说的是,从逻辑上讲,child 是从 Firebase 用户的 Uid 创建的 ID(代码在任何情况下都清楚)

现在假设我要检索用户的 PushId 并显示内容

referenceHistory = FirebaseDatabase.getInstance().getReference().child("Withdraw").child("????");

** 我必须在第二个 child 中输入什么才能检索从用户 ID 创建的 pushID?**

希望我解释得很好

为了能够获取对应于具有特定电子邮件地址的单个用户的数据,请使用以下代码行:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference withdrawRef = rootRef.child("Withdraw");
Query queryByEmail = withdrawRef.orderByChild("email").equalTo("example@gmail.com");
queryByEmail.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String id = ds.child("id").getValue(String.class);
                Log.d("TAG", id);
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

根据您的评论:

The first one interests me.

由于 children 具有相同的电子邮件地址,因此 logcat 中的结果将是:

-Mn1-FONHwx1-EmwZKND
-Mn1-ZI4lkbv4P76UMQl

如果您只想要第一个结果,您可以向现有查询添加一个 .limit(1) 调用,或者确保您有唯一的电子邮件地址。