Things have changed in 2021. Here is a more easy way. It takes the user from models asking what kind of type it is from interface then all good
var user models.User
query := bson.M{"$or": []bson.M{{"username": data["username"]}, {"email": data["username"]}}}
todoCollection := config.MI.DB.Collection(os.Getenv("DATABASE_COLLECTION_USER"))
todoCollection.FindOne(c.Context(), query).Decode(&user)
stringObjectID := user.ObjectID.Hex()
Above code works with this interface:
type User struct {
ObjectID primitive.ObjectID `bson:"_id" json:"_id"`
// Id string `json:"id" bson:"id"`
Username string `json:"username" gorm:"unique" bson:"username,omitempty"`
Email string `json:"email" gorm:"unique" bson:"email,omitempty"`
Password []byte `json:"password" bson:"password"`
CreatedAt time.Time `json:"createdat" bson:"createat"`
DeactivatedAt time.Time `json:"updatedat" bson:"updatedat"`
}
So consequently: this 3 line codes would do it nicely:
objectidhere := primitive.NewObjectID()
stringObjectID := objectidhere.Hex()
filename_last := filename_rep + "_" + stringObjectID + "." + fileExt