【问题标题】:Remove element from firestore array从 Firestore 数组中删除元素
【发布时间】:2020-03-28 18:45:35
【问题描述】:

我目前正在尝试从当前存储在 firestore 中的数组中删除一个元素。

我已经检查了所有地方,即使是 Firestore 文档,他们的建议也不起作用。 这是目前我的代码,到目前为止它在尝试从列表视图中删除时什么都不做。

         Map<String, Object> note = new HashMap<>();
         note.put("posts", FieldValue.delete());
         documentReference.update("posts", FieldValue.arrayRemove(listView.getItemAtPosition(position).toString()));
         usersEmotions.remove(listView.getItemAtPosition(position).toString());
         listView.setAdapter(listAdapter);
         listAdapter.remove(listView.getItemAtPosition(position).toString());

我的火力基地结构 -

【问题讨论】:

    标签: android firebase google-cloud-firestore


    【解决方案1】:

    如果您想从数组中删除一个项目,您的客户端应用程序需要传递该项目的整个对象值。如果您只传递该项目中的索引或嵌套值之一,它将不起作用。

    对于您的数据,这意味着您需要使用项目的确切内容(情感编号和文本字符串)构建一个 HashMap,并将其传递给 update()。例如:

    HashMap<String, Object> map = new HashMap<>();
    map.put(emotion, ...);
    map.put(text, "...");
    documentReference.update(posts, FieldValue.arrayRemove(map));
    

    如果您不知道整个项目的内容,这是行不通的。您唯一的其他选择是读取文档,修改内存中的列表,然后将修改后的列表写回文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-31
      • 1970-01-01
      • 2021-04-09
      • 2021-07-24
      • 2022-01-13
      • 2016-03-08
      • 2017-09-13
      相关资源
      最近更新 更多