diff --git a/app/mix/dao/mongo.go b/app/mix/dao/mongo.go index d79d0bb8..b98d5255 100644 --- a/app/mix/dao/mongo.go +++ b/app/mix/dao/mongo.go @@ -2319,7 +2319,7 @@ func (m *Mongo) CreateStreamer(ctx *gin.Context, streamer *dbstruct.Streamer) er func (m *Mongo) UpdateStreamer(ctx *gin.Context, streamer *dbstruct.Streamer) error { col := m.getColStreamer() - set := util.EntityToM(streamer) + set := util.DeepEntityToM(streamer) set["ut"] = time.Now().Unix() filter := qmgo.M{ "mid": util.DerefInt64(streamer.Mid), diff --git a/bizcommon/util/util.go b/bizcommon/util/util.go index c2ef77a3..44ee4f8f 100644 --- a/bizcommon/util/util.go +++ b/bizcommon/util/util.go @@ -35,6 +35,23 @@ func StringToMd5(s string) string { // 将实体类结构体指针转为bson.M,采用反射 func EntityToM(p any) qmgo.M { + set := qmgo.M{} + pType := reflect.TypeOf(p).Elem() + pVal := reflect.ValueOf(p).Elem() + for i := 0; i < pType.NumField(); i++ { + name := pType.Field(i).Tag.Get("bson") + if name != "" { + field := pVal.Field(i) + if name != "_id" && !field.IsNil() { + value := field.Elem().Interface() + set[name] = value + } + } + } + return set +} + +func DeepEntityToM(p any) qmgo.M { set := qmgo.M{} pType := reflect.TypeOf(p).Elem() pVal := reflect.ValueOf(p).Elem()