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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51202300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:00:14  来源:igfitidea点击:

How to Add/Update/Remove array elements in firebase firestore android using Hashmap? A Store Database

javaandroidfirebasegoogle-cloud-firestore

提问by sum20156

I want to make a collection of users, users will have multiple stores as documents, each document have fields like storeName, storeAddressand 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 availableProductarray using java in Android Studio?

我想创建一个用户集合,用户将有多个存储作为文档,每个文档都有诸如storeName,storeAddress和 之类的字段availableProducts。我的问题是如何管理 availableProducts 数组?我知道 Firestore 不能处理数组,我应该使用 HashMap。我的可用产品可能有产品名称、产品价格等字段。我是 Firebase 的新手,如何availableProduct在 Android Studio 中使用 java管理数组?

FireBase Firestore DataBase Image

FireBase Firestore 数据库图像

采纳答案by Alex Mamo

Edit: September 12, 2018

编辑:2018 年 9 月 12 日

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

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



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

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

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

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

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

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

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

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

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

看到您的数据库架构,我可以说您没有任何数组。这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());

Your price will be updated from 67.368to 63.121.

您的价格将从 更新67.36863.121

回答by Anga Koko

Good news guys, with the latest improvement to arrays you can add, update and remove an array element.

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

Check out this blog post: Better Arrays in Cloud Firestore!

查看此博文:Cloud Firestore 中的更好阵列!

You can do it like this

你可以这样做

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

回答by Manzur Alahi

This is how you can add a new item to an existing collection inside a document :

这是向文档内的现有集合添加新项目的方法:

FirebaseFirestore.getInstance().collection("COLLECTION_NAME").document("DOCUMENT_ID").update("NAME_OF_COLLECTION_NODE",FieldValue.arrayUnion(NEW_VALUE_OF_ANY_TYPE))

FirebaseFirestore.getInstance().collection("COLLECTION_NAME").document("DOCUMENT_ID").update("NAME_OF_COLLECTION_NODE",FieldValue.arrayUnion(NEW_VALUE_OF_ANY_TYPE))

check this link for more info: https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html

查看此链接了解更多信息:https: //firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html