java—在firestore中向单个文档添加2个哈希Map

uqzxnwby  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(309)

我已经上传了1个hashmap到firestore。当我尝试上传一个hashmap到同一个文档时,它会替换我以前的hashmap。为什么会这样?
每次我都会更改字符串标题,但它仍然会替换以前的hashmap。

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

mklgxw1f1#

您的文档需要说明,必须将其合并,而不是替换。

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

相关问题