3

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). 

Sample JSON:

{
    "field_one": "value",
    "data": {
        "field_two": [
            "data1",
            "data2"
        ],
        "field_three": "check",
        "field_four": "abc",
        "field_five": "work",
    }
}

I want to avoid hard coded field query for updating.

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.

1 Answers1

2

If you have control over the code, you could try creating methods on the struct. These methods can help you construct the fields path to perform partial update. For example, if you have the following structs:

type Outer struct {
    Data Inner  `bson:"data"`
}

type Inner struct {
    FieldThree string `bson:"field_three"`
    FieldFour string `bson:"field_four"`
}

You can try adding methods as below to construct update statements. These are returned in the dot-notation format.

func (o *Outer) SetFieldThree(value string) bson.E {
    return bson.E{"data.field_three", value}
}
func (o *Outer) SetFieldFour(value string) bson.E {
    return bson.E{"data.field_four", value}
} 

To update, you can construct the statements like below:

x := Outer{}
var updateFields bson.D
updateFields = append(updateFields, x.SetFieldThree("updated"))
updateFields = append(updateFields, x.SetFieldFour("updated"))

statement := bson.D{{"$set", updateFields}} 
result, err := collection.UpdateOne(ctx, bson.M{}, statement)
Wan B.
  • 18,367
  • 4
  • 54
  • 71
  • Got it solved by generating single level map from nested map [link](https://developer.mongodb.com/community/forums/t/nested-field-update-using-golang-struct/3398/5?u=ankush_goyal) – Ankush Goyal May 07 '20 at 12:29