nlthing
nlthing

Reputation: 17

Firestore many to many relationships - How to update data?

Like a note app, you can set tags to a note. Also, you can search notes with tag. Here is the firestore structure:

notes(collection)
    note_1(doc)
        content: 'some words...'
        tags: ['apple', 'banana']

tags(collection)
    tag_1(doc)
        name: 'apple'
    tag_2(doc)
        name: 'banana'

If I change tag_1.name to 'new_apple', how shold I update note_1.tags?

Upvotes: 0

Views: 159

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317497

You're going to have to:

  1. Query for all notes documents where the tags array contains "apple"
  2. Iterate the documents in the result set. For each document:
    1. Update update the tags array in memory to remove "apple" and add "new_apple"
    2. Write the new tags array back to the document

Upvotes: 1

Related Questions