英文:
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:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论