如何使用自定义结构在 mongo 中搜索?

How search in mongo with using custom structure?

如何忽略查询中time字段的默认值?
因为它们是在0001-01-01 00:00:00 +0000 UTC中设置的,我找不到合适的文档

// 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"`
}

示例https://play.golang.org/p/P2P30PPtl0

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 和空值,使用指向它的指针代替,即 *time.Timenil 指针值将是 值,任何非 nil 指针值将表示非空时间值。

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"`
}

参见相关问题: