drpjdfj618393 2017-12-18 05:11
浏览 65
已采纳

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

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

Example https://play.golang.org/p/P2P30PPtl0

  • 写回答

1条回答 默认 最新

  • doujing3896 2017-12-18 05:14
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部