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?
Edit: September 12, 2018
Starting with August 28, 2018
, now it's possible to update array members. More informations here.
How to Add/Update/Remove array elements in firebase firestore?
The short answer is that you cannot! As in the official documentation regarding arrays:
Although Cloud Firestore can store arrays,
it does not support
querying array members or updating single array elements.
So there is currently no way to add, update or remove a single array element in a Cloud Firestore database.
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());
Your price will be updated from 67.368
to 63.121
.