Skip to content

Append to an arary field in Firestore

I’m using Firebase and Vuejs to create an database element, which has object array inside.

enter image description here

That’s how the field looks, and I want to add tasks through the form into the ‘moreTasks’ as an array.

I tried using this, but it just creates new entity in the database.

db.collection('Tasks').add({
    tasker: this.tasker.taskerName
})

I also tried checking API but I couldnt understand the refs, because I was using different methods to achieve that goal.

creatTask() {
  db.collection('Tasks').add({
    task_id: this.task_id,
    name: this.name,

What would be correct way to approach this problem?

Answer

You can append an item to an array using FieldValue.arrayUnion() as described in the documentation. For example:

// Atomically add a new region to the "regions" array field.
washingtonRef.update({
    regions: firebase.firestore.FieldValue.arrayUnion("greater_virginia")
});