Java 在Firestore中向单个文档添加2个哈希映射
我已将1个Hashmap上载到firestore。当我尝试将另一个Hashmap上载到同一文档时,它将替换我以前的Hashmap。为什么会这样 我每次都会更改字符串标题,但它仍然会替换以前的HashmapJava 在Firestore中向单个文档添加2个哈希映射,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我已将1个Hashmap上载到firestore。当我尝试将另一个Hashmap上载到同一文档时,它将替换我以前的Hashmap。为什么会这样 我每次都会更改字符串标题,但它仍然会替换以前的Hashmap String poll1 = Poll1.getText().toString().trim(); String poll2 = Poll2.getText().toString().trim(); String poll3 = Poll3.ge
String poll1 = Poll1.getText().toString().trim();
String poll2 = Poll2.getText().toString().trim();
String poll3 = Poll3.getText().toString().trim();
String title = Title.getText().toString().trim();
DocumentReference documentReference = firestore.collection("Polls").document("abc1");
Map<String, Object> nestedData = new HashMap<>();
nestedData.put(poll1 , 0 );
nestedData.put(poll2 , 0);
nestedData.put(poll3 , 0);
Map<String, Object> upload = new HashMap<>();
upload.put(title, nestedData);
documentReference.set(upload).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(polls.this, "UPLOADED", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(polls.this, "FAILED", Toast.LENGTH_LONG).show();
}
});
您的文档需要提及的是,必须将其合并,而不是替换
.set(data, SetOptions.merge())
如果有用的话,试试这个
documentReference.set(upload, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(polls.this, "UPLOADED", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(polls.this, "FAILED", Toast.LENGTH_LONG).show();
}
});