如何使用Hashmap在firebase firestore android中添加/更新/删除数组元素?商店数据库

sum*_*156 7 java android firebase google-cloud-firestore

我想制作一个用户集合,用户将有多个商店作为文档,每个文档都有类似的字段storeName,storeAddressavailableProducts.我的问题是如何管理availableProducts数组?我知道Firestore无法处理数组,我应该使用HashMap.我的可用产品可能包含产品名称,产品价格等字段.我是Firebase的新手,如何availableProduct在Android Studio中使用java 管理数组?

FireBase Firestore数据库图像

Ang*_*oko 15

好消息,随着阵列的最新改进,您可以添加,更新和删除数组元素.

看看这篇博文:Cloud Firestore中的更好阵列!

你可以这样做

//Map to add user to array
final Map<String, Object> addUserToArrayMap = new HashMap<>();
addUserToArrayMap.put("arrayOfUsers", FieldValue.arrayUnion(mAuth.getCurrentUser().getUid()));

//Map to remove user from array
final Map<String, Object> removeUserFromArrayMap = new HashMap<>();
removeUserFromArrayMap.put("arrayOfUsers", FieldValue.arrayRemove(mAuth.getCurrentUser().getUid()));

//use either maps to add or remove user
db.collection("REFERENCE").document("mDocumentId")
                .update(addUserToArrayMap);
Run Code Online (Sandbox Code Playgroud)


Ale*_*amo 8

编辑:2018 年 9 月 12 日

从 开始August 28, 2018,现在可以更新数组成员。更多信息在这里


如何在 firebase firestore 中添加/更新/删除数组元素?

简短的回答是你不能!如关于数组的官方文档

虽然 Cloud Firestore 可以存储数组、it does not support查询数组成员或更新单个数组元素。

因此,目前无法在 Cloud Firestore 数据库中添加、更新或删除单个数组元素。

看到您的数据库架构,我可以说您没有任何数组。这availableProducts是一个对象,在它下面有一个名为的地图0,其中包含两个 String 属性,spNamespPrice. 如果你想更新,说价格,请使用以下代码:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference ref = rootRef.collection("gdgsghs.cok").document("Shsheg");
Map<String, Object> availableProducts = new HashMap<>();
Map<String, Object> zeroMap = new HashMap<>();
Map<String, Object> product = new HashMap<>();
product.put("spPrice", 63.121);
zeroMap.put("0", product);
availableProducts.put("availableProducts", zeroMap);
ref.set(availableProducts, SetOptions.merge());
Run Code Online (Sandbox Code Playgroud)

您的价格将从 更新67.36863.121