0

I have a Firebase array that looks like this

categories:[{rating: 1, name: 'abc'}, {rating: 2, name: 'xyz'}]

How can I update the element with name 'abc' for example to make it something like this {rating: 3, name: 'abc'}

I tried the below but it adds an element to the array as it considers it unique due to the different rating value.

var ref = _db.collection('reports').doc(user.uid);
const data = { categories: FieldValue.arrayUnion([{"name": 'abc', "rating": 3}]) } 
ref.set(data, SetOptions(merge: true));
Teknoville
  • 459
  • 1
  • 7
  • 18

1 Answers1

4

You need to read the Array, modify it in your front-end and write it back to Firestore (i.e. update the document by passing the entire modified array).

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121