1

I want to update a field of an object that the object is in an array in the Firestore database with Swift 4. These two functions (arrayUnion() and arrayRemove() ) are not working for me.

Here my Database schema:

ScreenShot

I want to update the "Status" field in The first array element.

4
  • 1
    Go ahead and add some of your code so we can better help you. Commented May 13, 2019 at 19:35
  • @JoeyPhillips The post has been updated. Commented May 14, 2019 at 4:34
  • He meant to add code to the question. Please don’t include images or links in your questions. Include code and structures as text. To get your Firebase structure, use the Firebase console->Export JSON and copy and paste a snippet of your structure. See images and links are evil. To get code, please copy and past the textual code into the question and format for readability using the {} in the toolbar. Also, please clarify the question as it's doesn't make sense as is. Commented May 14, 2019 at 19:25
  • While @dougstevenson has a good answer, another option would be to just update the specific field instead of reading the entire document, modifying it and writing it back out. So you can check out both answers and see which one fits the best for your use case. Commented May 18, 2019 at 14:15

3 Answers 3

1

first of all I want to thanks to @Doug Stevenson for his kindly response. in my case, I must change the array of the objects to sub collections.

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

1 Comment

I am not sure why this was posted as an answer as it doesn't answer the question of how to update an object (field). Secondly, changing that array to a sub collection is probably not the best option; you should change the users to be stored as documents within a collection (/users) using the uid as the documentID (key) for each users node.
1

I believe the question is:

how can I update a specific field that's stored within a document in an array.

As long as you know the documentId to the document you want to update - here's the solution.

Assuming a structure similar to what's in the question

Friends (a collection)
   0 (a document)
      Name: "Simon"
      Status: "Sent"
   1
      Name: "Garfunkle"
      Status: "Unsent"

and say we want to change Garfunkle's status to Sent

I will include one function to read document 1, Garfunkle's and then a second fuction to update the Status to sent.

Read the document at index 1 and print it's fields - this function is just for testing to show the field's values before and after changing the status field.

func readFriendStatus() {
    let docRef = self.db.collection("Friends").document("1")
    docRef.getDocument(completion: { document, error in
        if let document = document, document.exists {
            let name = document.get("Name") ?? "no name"
            let status = document.get("Status") ?? "no status"
            print(name, status)
        } else {
            print("no document")
        }
    })
}

and the output

Garfunkle Unsent

then the code to update the Status field to Sent

func writeFriendStatus() {
    let data = ["Status": "Sent"]
    let docRef = self.db.collection("Friends").document("1")
    docRef.setData(data, merge: true)
}

and the output

Garfunkle, Sent

Comments

0

In a transaction, fetch the document, modify the field of the object as you see fit in memory on the client, then update that entire field back to the document.

Comments

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.