1
votes

I'm trying to update nested object in google's Firebase Cloud Firestore. My database structure is:
collection 'users'
-- documents (userId)
---- collection 'matrix
------ document 'service' (got more documents: same type different names)
-------- success: Story[]
-------- failure: Story[]
-------- name: string

Story Model:
title: string text: string storyType: string ('success' | 'failure) case: string (for instance 'service') ...

now I'm using Angular and I want to update a single story using. How do I do that?
I know how to get the reference to the 'service' document

update(story: Story, userId:string){ }

db.collection('users').doc(userId).collection('matrix).doc(story.case).update()
Do i need to update the whole document or can I reach the specific story by the story object (I know his type for instance 'success' and title is uniq) so I can search which has this title and update only this story?

1

1 Answers

1
votes

You can use arrayUnion and arrayRemove operators to add and remove elements from the array.

for example, to add a Story

  db.collection('users').doc(userId).collection('matrix).doc(story.case).update(
    {"success": FieldValue.arrayUnion(someStory)}
    )

these operators treat array like sets. you can not access individual item by index.

official documentation here:https://firebase.google.com/docs/firestore/manage-data/add-data