Firestore&Java:删除数组中的单个元素

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

Firestore & Java: Remove a single element of Array

问题

我正在尝试删除 Firestore 数组中的单个元素。数据库具有以下结构:

(图片链接已省略)

不幸的是,使用以下代码不会产生任何效果。

public static void deleteTable(final int pos){
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    FirebaseAuth fAuth = FirebaseAuth.getInstance();

    DocumentReference docRef = db.collection("users").document(fAuth.getUid().toString());
    docRef.update("myTables", FieldValue.arrayRemove(pos));
}

如果有人能帮我解决这个问题,我会很高兴。
我只想从 myTables 数组中删除一个元素。(这些元素是 HashMap)

谢谢,Andi

英文:

I'm trying to delete a single element of an Firestore array. The database has the following structure:

Firestore&Java:删除数组中的单个元素

Unfortunately, with the following code nothing happens.

public static void deleteTable(final int pos){
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    FirebaseAuth fAuth = FirebaseAuth.getInstance();

    DocumentReference docRef = db.collection("users").document(fAuth.getUid().toString());
    docRef.update("myTables", FieldValue.arrayRemove(pos));
}

It would be nice if someone could help me out with this one.
I just want to delete one element out of the myTables array. (The elements are HashMaps)

Cheers Andi

答案1

得分: 1

Firestore不支持通过索引修改数组字段。FieldValue.arrayRemove() 必须始终接收要移除项的完整内容。在您的情况下,这将是一个包含要移除项中所有嵌套字段的映射(Map)。

如果您想按索引移除字段,您需要读取文档,在内存中修改数组,然后将修改后的数组写回文档。

英文:

Firestore does not support modification of array fields by index. FieldValue.arrayRemove() must always be delivered the full contents of the item to remove. In your case, that would be a Map containing all of the nested fields in the item to remove.

If you want to remove a field by index, you have to read the document, modify the array in memory, then write the modified array back to the document.

huangapple
  • 本文由 发表于 2020年10月25日 02:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64516784.html
匿名

发表评论

匿名网友

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

确定