Updating a document with a custom object (struct)

Viewed 201

We can use a struct to both create and get a document form firestore, but the documentation for updating a document doesn't show that it's possible to use a custom object (struct) here. Instead we have to re-assign each field, but this means we don't know which fields are updating?

What I expect to work


document.Name = "John Smith" // Could be any field, just an example

ctx := context.Background()
_, err = c.Collection("people").Doc(document.ID).Update(ctx, &document)
if err != nil {
    return err
}

What it says I need to do

document.Name = "John Smith"

ctx := context.Background()
_, err = c.Collection("people").Doc(document.ID).Update(ctx, []fire.Updates{
    {
        Name:  document.name
    },
})
if err != nil {
    return err
}

What if I don't know what has been updated? or what if multiple things in the object have updated? I might have to manually assign each of the structs attributes in the update?

0 Answers
Related