I'm working with golang and the MongoDB driver, I want to patch one of my objects according to the data I get from the outside:
I have a struct:
type Pivot struct { Email string `json:"email"` Base string `json:"base"` } And the patch (with MongoDB Update)
setMap := bson.D{ {"$set", setElements}, } res, err := collection.UpdateMany( ctx, filter, setMap, ) And I want to make the setObject a little bit dynamic:
if len(pivot.Base) > 0 { setElements.append("base", pivot.Base) //this doesn't work... } if len(pivot.Email) > 0 { setElements.append("email", pivot.Email) } I' ve seen that the setObject can be built like
{"$set", bson.D{ {"processed", pivot.Processed}, } But how can I make it dynamic?