1

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

This worked for add/ delete but if I change only the property 'text' of the desired story it will add new story instead of updating the old one. I can remove the old and add the new edited story but I don't know if thats a good idea
I think there's no update function from firebase API for this situation.

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.