I am having some trouble with the mongodb driver for Go when trying to decode a property in my collection which is of type string to a property in my struct which is of type int. I have a collection that has a property that is supposed to be of type int that was converted to type string by someone editing a single document. Since all of the documents have that property as int except that one, I am getting an error "cannot decode string into an integer type". I figured out a way to do it but it's not something that "should" be done. I modified the IntDecodeValue func in the default_value_decoders.go in the mongodb driver for Go. Below is what I added.
case bsontype.String:
s, err := vr.ReadString()
if err != nil {
return err
}
i32, err := strconv.Atoi(s)
if err != nil {
return err
}
i64 = int64(i32)
I know this will be overwritten when I update the driver but have been beating my head against the wall trying to figure out how to handle such a case. I know the best solution is to not allow editing of the documents directly but want to account for that case.