type User struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
CreatedAt time.Time `bson:"created_at"`
UpdatedAt time.Time `bson:"updated_at"`
Name string `bson:"name"`
}
user := User{Name: "username"}
client.Database("db").Collection("collection").InsertOne(context.Background(), user)
How to use automated created_at and updated_at in the above code with mongodb(mongodb driver only) in golang? Currently it will set zero time(0001-01-01T00:00:00.000+00:00) for created_at and updated_at.
The MongoDB server does not support this.
You may implement a custom marshaler in which you may update these fields to your liking. Implement
bson.Marshaler
, and yourMarshalBSON()
function will be called when you save values of your*User
type.This is how it would look like:
Note the method has pointer receiver, so use a pointer to your value:
The purpose of the
my
type is to avoid stack overflow.