以前,我使用实时数据库,但现在我需要更改为使用firestore。
当我在实时数据库中写数据时,我使用这种方法。
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String userid = firebaseUser.getUid();
reference = FirebaseDatabase.getInstance().getReference().child("Users").child(userid);
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("id", userid);
hashMap.put("email", email);
hashMap.put("username", username);因此,数据将如下所示
"Users" : {
"02nfE2JziDgzxZwYpmCwQ20QwQ93" : {
"email" : "sowtp12pushcivet@gmail.com",
"id" : "02nfE2JziDgzxZwYpmCwQ20QwQ93",
"username" : "AAA"
},但是当我使用firestore时,它会显示以下内容
"Users" : {
"u93AclSJJiLQBdfcK2st" : {
"email" : "sowtp12pushcivet@gmail.com",
"id" : "02nfE2JziDgzxZwYpmCwQ20QwQ93",
"username" : "AAA"
},我可以将u93AclSJJiLQBdfcK2st更改为02nfE2JziDgzxZwYpmCwQ20QwQ93吗?如果它可以更改,我该怎么办?
这是我的代码。
db = FirebaseFirestore.getInstance();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("id", userid);
hashMap.put("email", email);
hashMap.put("username", username);
db.collection("Users").add(hashMap).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(RegisterActivity.this, "OK", Toast.LENGTH_SHORT).show();
}
});发布于 2021-11-03 17:00:25
当您调用add()时,Firestore会生成一个新的文档ID。它等同于为实时数据库API调用push()。
如果您想指定自己的文档ID,请不要调用add,而是使用document("yourNewId").set()传递:
db.collection("Users").document(userid).set(hashMap)...另请参阅setting a document value上的Firebase文档。
https://stackoverflow.com/questions/69828258
复制相似问题