0

I know how to update an array of objects like this:- (Note: This is the field in my Firestore DB)

details: [
{para:'Something..'},
{para:'Something again...'}
]

It will be done like this:-

db.collection(myCollection).doc(myID).update({
    details: firebase.firestore.FieldValue.arrayUnion({
        para: 'This will get added'
        })
    })

But What if the array of objects is inside another object like this??

details: {
    descrip: [
        {para:'Something..'}
    ]
}

When I try to do like this:-

db.collection(myCollection).doc(myID).update({
        details: {
            descrip: firebase.firestore.FieldValue.arrayUnion({
                para: 'This will get added'
            })
        }
        })

It removes the old para('Something..') and replaces it with the new one('This will get added').

I want this to happen:-

details: {
    descrip: [
        {para:'Something..'},
        {para:'This will get added'}
    ]
}

Is there any way to do it??

You may ask that if there is an easy way to do it(The above one) then why doing this. Actually this is just an example to get an idea of how it works. I am working on nested arrays of objects.

1 Answer 1

1

The following should do the trick:

  db.collection('myCollection')
    .doc('myID')
    .update({
      ['details.descrip']: firebase.firestore.FieldValue.arrayUnion({
        para: 'This will get added',
      }),
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Yes it is. Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.