![](/img/trans.png)
[英]How to retrieve data from a map inside a particular document of firestore
[英]How to add data to the array of hash map in the document using Firestore?
我想在不覆盖的情况下将数据添加到现有数组。 在每个文档中,我有一个HashMap
数组。 我正在尝试向现有数据添加数据。 请检查下面的代码并说明一些问题。
public void createNewCase(){
Map<String, String> caseInfo = new HashMap<String, String>();
caseInfo.put("chief_complaint", chiefComplaintET.getText().toString());
caseInfo.put("facility", facilityET.getText().toString());
caseInfo.put("location", locationET.getText().toString());
caseInfo.put("assigned_provider", assignProviderET.getText().toString());
caseInfo.put("referring_provider", referringProviderET.getText().toString());
caseInfo.put("admit_date", adminDateET.getText().toString());
caseDictionary.add(caseInfo);
final String patientID = sharedPrefHelper.getStr("patient_id");
db.collection("patients").whereEqualTo("patient_id", patientID).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
private static final String TAG = "New Case Creation" ;
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
List<HashMap<String,String>> list = new ArrayList<>();
for (QueryDocumentSnapshot document : task.getResult()) {
patient patient = new patient();
list = (List) document.get("patient_case");
for (HashMap<String, String> item:list) {
caseDictionary.add(item);
}
}
System.out.println(caseDictionary);
HashMap<String, Object> uploadData = new HashMap<>();
uploadData.put("patient_case", caseDictionary);
DocumentReference caseRef = db.collection("patients").document(patientID); // am stuck here
}else{
Log.w(TAG, "Error getting documents.", task.getException());
Toast.makeText(NewCase.this, "Something bad happened", Toast.LENGTH_SHORT).show();
Helper.m_Dialog.hide();
}
}
});
}
编辑 1下面的代码删除旧数据并添加新数据。 我需要追加。
final String patientID = sharedPrefHelper.getStr("patient_id");
final CollectionReference collectionReference = db.collection("patients");
collectionReference.whereEqualTo("patient_id", patientID).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
Toast.makeText(NewCase.this, task.getResult().toString(), Toast.LENGTH_SHORT).show();
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Map<Object, Object> map = new HashMap<>();
map.put("patient_case", caseInfo);
collectionReference.document(document.getId()).set(map, SetOptions.merge());
}
}else{
Toast.makeText(NewCase.this, task.getResult().toString(), Toast.LENGTH_SHORT).show();
}
}
});
正如我在您的屏幕截图中看到的,在您的patients
集合中,您的文档包含一个包含地图(对象)的数组。 为了能够更新数组中存在的那些映射,您应该创建一个与您的确切文档结构相对应的Map
对象,并为此使用 DocumentReference 的set(Object data, SetOptions options)方法。 因此,将地图作为第一个参数传递,并将SetOptions.merge()
作为第二个参数传递。 您可以在我的以下帖子的回答中找到一个更简单的示例:
这是我的例子之一
我已经这样做了
List<Map> history = List(); History historyObj = History(typee, name, timeStamp, pointsBefore, points, itemPoints); history.add(historyObj.toMap()); //Firesore collection object CollectionReference child = _firestore.collection('children'); //Firesore document object DocumentReference docRef = child.doc(selectedChild.childId); // "history" is your "patient_case" docRef.update({"history": FieldValue.arrayUnion(history)}).then( (value) => print("Update done"));
历史是我的一门课
History 类包括 toMap() 方法,该方法将所有历史类变量及其值转换为 'Key' : 'value' 形式,如下所示
Map<String, dynamic> toMap() => { "type": type, "name": name, "timestamp": timestamp, "points_before": points_before, "points_after": points_after, "points_affected": points_affected, };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.