I have a Go application, which uses Firestore to store data.
When updating a document, I’m passing a map of data to Firestore, but it behaves differently, depending on whether I’m using firestore.MergeAll
, or firestore.Merge(...)
.
I have the following struct:
type User struct {
Tokens Tokens
}
type Tokens struct {
Sessions map[string]Token
}
type Token struct {
Token string
Expires int
}
In certain situation, I’d like to clear the Sessions
property of any values, and I do it like so:
dataMap := map[string]interface{}{
"Tokens": map[string]interface{}{
"Sessions": map[string]interface{}{},
},
}
_, err = c.ref.Doc(id).Set(ctx, dataMap, firestore.MergeAll)
// dataMap looks like so: map[Tokens:map[Sessions:map[]]]
When I do this, I get a Firestore error: firestore: missing WriteResult
.
However, if I update it like below, it works as expected, and Sessions
becomes an empty map inside Firestore…
dataMap := map[string]interface{}{
"Tokens": map[string]interface{}{
"Sessions": map[string]interface{}{},
},
}
_, err = c.ref.Doc(id).Set(ctx, dataMap, firestore.Merge(firevault.FieldPath{"Tokens", "Sessions"}))
// dataMap looks like so: map[Tokens:map[Sessions:map[]]]
What is the difference between the two? Is there a way to achieve the same functionality with firestore.MergeAll
, so that I don’t always have to pass a FieldPath
?