How to ignore the default values of the time
field in the query?
Because they are set in 0001-01-01 00:00:00 +0000 UTC
, I can not find the right document
// User model
type User struct {
Mail string `json:"mail" bson:"mail,omitempty"`
Password string `json:"password" bson:"password,omitempty"`
CreatedAt time.Time `json:"created_at" bson:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at" bson:"updated_at,omitempty"`
}
time.Time
is a struct type, and its zero value is a valid time value and is not considered "empty". So in case of time.Time
if you need to differentiate between the zero and empty values, use a pointer to it instead, that is *time.Time
. The nil
pointer value will be the empty value, and any non-nil
pointer values will denote non-empty time values.
type User struct {
Mail string `json:"mail" bson:"mail,omitempty"`
Password string `json:"password" bson:"password,omitempty"`
CreatedAt *time.Time `json:"created_at" bson:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at" bson:"updated_at,omitempty"`
}
See related question: Golang JSON omitempty With time.Time Field