向Firestore文档添加两个哈希映射

huangapple go评论101阅读模式
英文:

Adding 2 Hashmaps to a single document in Firestore

问题

我已经将一个Hashmap上传到了Firestore。当我尝试上传另一个Hashmap到同一个文档时,它会替换掉之前的Hashmap。为什么会这样呢?

我每次都更改String标题,但它仍然会替换掉之前的Hashmap。

  1. String poll1 = Poll1.getText().toString().trim();
  2. String poll2 = Poll2.getText().toString().trim();
  3. String poll3 = Poll3.getText().toString().trim();
  4. String title = Title.getText().toString().trim();
  5. DocumentReference documentReference = firestore.collection("Polls").document("abc1");
  6. Map<String, Object> nestedData = new HashMap<>();
  7. nestedData.put(poll1, 0);
  8. nestedData.put(poll2, 0);
  9. nestedData.put(poll3, 0);
  10. Map<String, Object> upload = new HashMap<>();
  11. upload.put(title, nestedData);
  12. documentReference.set(upload).addOnCompleteListener(new OnCompleteListener<Void>() {
  13. @Override
  14. public void onComplete(@NonNull Task<Void> task) {
  15. Toast.makeText(polls.this, "已上传", Toast.LENGTH_LONG).show();
  16. }
  17. }).addOnFailureListener(new OnFailureListener() {
  18. @Override
  19. public void onFailure(@NonNull Exception e) {
  20. Toast.makeText(polls.this, "上传失败", Toast.LENGTH_LONG).show();
  21. }
  22. });
英文:

I have uploaded 1 Hashmap to firestore.And when i try to upload one more Hashmap to the same document it replaces my previous Hashmap.Why is it so?

I change the String title everytime and still it replaces the previous Hashmap.

  1. String poll1 = Poll1.getText().toString().trim();
  2. String poll2 = Poll2.getText().toString().trim();
  3. String poll3 = Poll3.getText().toString().trim();
  4. String title = Title.getText().toString().trim();
  5. DocumentReference documentReference = firestore.collection(&quot;Polls&quot;).document(&quot;abc1&quot;);
  6. Map&lt;String, Object&gt; nestedData = new HashMap&lt;&gt;();
  7. nestedData.put(poll1 , 0 );
  8. nestedData.put(poll2 , 0);
  9. nestedData.put(poll3 , 0);
  10. Map&lt;String, Object&gt; upload = new HashMap&lt;&gt;();
  11. upload.put(title, nestedData);
  12. documentReference.set(upload).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
  13. @Override
  14. public void onComplete(@NonNull Task&lt;Void&gt; task) {
  15. Toast.makeText(polls.this, &quot;UPLOADED&quot;, Toast.LENGTH_LONG).show();
  16. }
  17. }).addOnFailureListener(new OnFailureListener() {
  18. @Override
  19. public void onFailure(@NonNull Exception e) {
  20. Toast.makeText(polls.this, &quot;FAILED&quot;, Toast.LENGTH_LONG).show();
  21. }
  22. });

答案1

得分: 1

你的文档需要提到,这必须合并而不是替换。

  1. .set(data, SetOptions.merge())

如果有效,尝试这样做。

  1. documentReference.set(upload, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
  2. @Override
  3. public void onComplete(@NonNull Task<Void> task) {
  4. Toast.makeText(polls.this, "已上传", Toast.LENGTH_LONG).show();
  5. }
  6. }).addOnFailureListener(new OnFailureListener() {
  7. @Override
  8. public void onFailure(@NonNull Exception e) {
  9. Toast.makeText(polls.this, "上传失败", Toast.LENGTH_LONG).show();
  10. }
  11. });
英文:

your document needs to mention that this has to be merged instead replace.

  1. .set(data, SetOptions.merge())

Try this if it works.

  1. documentReference.set(upload, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
  2. @Override
  3. public void onComplete(@NonNull Task&lt;Void&gt; task) {
  4. Toast.makeText(polls.this, &quot;UPLOADED&quot;, Toast.LENGTH_LONG).show();
  5. }
  6. }).addOnFailureListener(new OnFailureListener() {
  7. @Override
  8. public void onFailure(@NonNull Exception e) {
  9. Toast.makeText(polls.this, &quot;FAILED&quot;, Toast.LENGTH_LONG).show();
  10. }
  11. });

huangapple
  • 本文由 发表于 2020年8月20日 17:45:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63502455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定