2017-12-18 8 views
2

クエリのtimeフィールドのデフォルト値を無視するにはどうすればよいですか?彼らは0001-01-01 00:00:00 +0000 UTCに設定されているので
は、私がカスタム構造を使用してmongoで検索するにはどうすればよいですか?

// 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

答えて

3

time.Timeがある右の文書構造体型を見つけることができない、とそのzero値は「空の有効な時間の値であると見なされていません"だからtime.Timeの場合は、zeroと空の値を区別する必要がある場合は、その代わりにポインタを使用してください。つまり、*time.Timeです。 nilポインタの値はの値がとなり、ポインタの値がnilでない場合は、空ではない時間の値が表示されます。 Golang JSON omitempty With time.Time Field

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

は、関連の質問を参照してください
関連する問題