如何使用Firestore将数据添加到文档中的哈希映射数组中? [英] How to add data to the array of hash map in the document using Firestore?

查看:75
本文介绍了如何使用Firestore将数据添加到文档中的哈希映射数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不覆盖的情况下将数据添加到现有阵列中.在每个文档中,我都有一个 HashMap 数组.我正在尝试将数据添加到现有数据中.请检查下面的代码并阐明一些内容.

I want to add data to the existing array without overwriting. And inside each document, I have an array of HashMap. I am trying to add data to the existing one. Kindly check the below code and shed some light.

   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 下面的代码删除旧数据并添加新数据.我需要附加.

Edit 1 Below code deleting old data and adding new data. I need to append.

   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();
            }
        }
    });

推荐答案

正如我在屏幕快照中看到的那样,在您的患者集合中,您的文档中包含一个包含地图(对象)的数组.为了能够更新数组中存在的那些地图,您应该创建一个与您的确切文档结构相对应的 Map 对象,并使用DocumentReference的

As I see in your screenshot, in your patients collection you have documents that contain an array that holds maps (objects). In order to be able to update those maps that exist within your array you should create a Map object that corresponde to your exact document structure and use DocumentReference's set(Object data, SetOptions options) method for that. So pass as the first argument the map and as the second argument SetOptions.merge(). You can find a simpler example in my answer from the following post:

这篇关于如何使用Firestore将数据添加到文档中的哈希映射数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆