15

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 DataBase Image

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
sum20156
  • 646
  • 1
  • 7
  • 19
  • Are you asking how do you gather that data and put it into an Array or should you use a Map instead? Or how do you use a Map for that snapshot? – Torewin Jul 06 '18 at 03:43
  • should i use map or subcollection? – sum20156 Jul 06 '18 at 03:57
  • Personally, I would use a Map as it makes more sense and easier. Which way is better, is up for interpretation. You might want to create a class of availableProduct, but the Map works just as well. – Torewin Jul 06 '18 at 04:01

3 Answers3

25

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!

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);
Anga
  • 2,450
  • 2
  • 24
  • 30
  • really appreciate it man. This is perfect for a post voting system where there are two options up or down. But how would you do the query to get the final number of voters for each state (up or down)? I'm currently adding one whenever the users presses a vote but that isn't perfect obviously – speedox Dec 01 '19 at 16:21
  • @speedox It seems what you want is data at real time. This is a link to firebase documentation to achieve that https://firebase.google.com/docs/firestore/query-data/listen?authuser=0#view_changes_between_snapshots – Anga Dec 02 '19 at 12:33
9

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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks for answering, but how to retrieve the product price? – sum20156 Jul 07 '18 at 03:10
  • This is basically another question. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you. – Alex Mamo Jul 07 '18 at 07:19
2

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

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

Manzur Alahi
  • 1,870
  • 23
  • 19