如何使用Hashmap在Firebase Firestore Android中添加/更新/删除数组元素?商店数据库 [英] How to Add/Update/Remove array elements in firebase firestore android using Hashmap? A Store Database

查看:88
本文介绍了如何使用Hashmap在Firebase Firestore Android中添加/更新/删除数组元素?商店数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make a collection of users, users will have multiple stores as documents, each document have fields like storeName, storeAddress and availableProducts. My question is that how to manage availableProducts array? I know Firestore can't handle arrays, I should use HashMap. My available products may have field like product name, product price etc. I am new in Firebase, how can I manage availableProduct array using java in Android Studio?

FireBase Firestore数据库图像

推荐答案

2018年9月12日

August 28, 2018开始,现在可以更新数组成员.有关更多信息,请 此处 .

Starting with August 28, 2018, now it's possible to update array members. More informations here.

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

How to Add/Update/Remove array elements in firebase firestore?

简短的答案是您不能!如关于数组的官方文档所示:

The short answer is that you cannot! As in the official documentation regarding arrays:

尽管Cloud Firestore可以存储阵列,但 it does not support 可以查询阵列成员或更新单个阵列元素.

Although Cloud Firestore can store arrays, it does not support querying array members or updating single array elements.

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

So there is currently no way to add, update or remove a single array element in a Cloud Firestore database.

看到您的数据库模式,我可以说您没有任何数组. availableProducts是一个对象,在其下方有一个名为0的映射,其中包含两个String属性,分别为spNamespPrice.如果您想更新价格,请使用以下代码:

Seeing your database schema I can say that you don't have any arrays. The availableProducts is an object, beneath it there is a map named 0 which holds two String properties, spName and spPrice. If you want to update, let's say the price, please use the following code:

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());

您的价格将从67.368更新为63.121.

这篇关于如何使用Hashmap在Firebase Firestore Android中添加/更新/删除数组元素?商店数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆