I want to create an array programmatically in firestore. Also, I want to update the array as shown in the image attached.
I want to store all reference Numbers in this array. And whenever there is a new reference number, I want to update the array. Please help.
This is what I have tried. I know it's wrong. It's not updating the array rather replacing it.
Map<String, Object> mapone= new HashMap<>();
Map<String, Object> maptwo = new HashMap<>();
mapone.put("Refnum", f_refrenceNum);
maptwo.put("RefNumber", mapone);
upiRefnum.set(maptwo,SetOptions.merge());
CodePudding user response:
Your code is telling Firestore to the RefNumber.Refnum
fields with the values you specify for is.
If you want to add f_refrenceNum
to the array, you can use an array-union
operation:
mapone.put("Refnum", FieldValue.arrayUnion(f_refrenceNum));
This will add f_refrenceNum
if it isn't already in the array. If it is already in the array and you want to add a duplicate, you will have to read the document into your application code, add the number in your application code, and then write the entire array back to the database.
See the Firestore documentation on adding items to an array.