java - 如何使用Firestore将数据添加到文档中的哈希映射数组?-LMLPHP

我想在不覆盖的情况下将数据添加到现有阵列。在每个文档中,我都有一个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)方法。因此,将map作为第一个参数传递,并将第二个参数SetOptions.merge()传递。您可以从以下帖子的答案中找到一个更简单的示例:


Firestore firebase Android search and update query

08-18 08:30
查看更多