在我的情况下,我有一个Map类型的数组,名称是players。当我从应用程序中单击加入按钮时,我希望新的player添加到该数组中。
Map<String,Object> playerMap=new HashMap<>();
playerMap.put("name","adnan");
playerMap.put("subtitle","bashir");
playerMap.put("image","url");
playerMap.put("documentID","id2");
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("matchesPRO").document("5mr4xk7I3InAe8LR2SbJ").update("players",playerMap);
最佳答案
如果只想向数组添加项目,则可以使用arrayUnion()
方法,如下所示:
DocumentReference docRef = db.collection("matchesPRO").document("docID");
// Atomically add a new player Map to the "players" array field.
docRef.update("players", FieldValue.arrayUnion(playerMap));