I am facing a issue with update document using golang mongo driver.
Scenario: I want to update a field that is nested in a struct. For ex: StructOuter -> structInner -> field1, field2, field3. Now if I want to update the field3 and I have the corresponding value as another struct, how can i go ahead by just updating this field alone. I tried with code below but it updates the whole structInner leaving only field3:
conv, _ := bson.Marshal(prod)
bson.Unmarshal(conv, &updateFields)
update := bson.M{
"$set": updateFields,
}
model.SetUpdate(update).
Adding Example:
Current Doc:
{
"field_one": "value",
"data": {
"field_two": [
"data1",
"data2"
],
"field_three": "check",
"field_four": "abc",
"field_five": "work",
}
}
Update Request:
{
"data": {
"field_three": "check changed"
}
}
Result Expected:
{
"field_one": "value",
"data": {
"field_two": [
"data1",
"data2"
],
"field_three": "check changed",
"field_four": "abc",
"field_five": "work",
}
}
Result Got:
{
"field_one": "value",
"data": {
"field_three": "check changed",
}
}
Field changed is data.field_three. The update request has been populated in struct. but when the bson masrshall and unmarshall is used to form bson.M from it, the map created is multiple nested.
Just want to know if this is supported, if yes can you help me with it and also point to some deep dive links on this.