Came across a problem with encoding *time.Time. It turned out that it is ignored during encoding process. Code to reproduce: package main import ( "log" "time" "github.com/mongodb/mongo-go-driver/bson" ) type testStruct struct { FieldA time.Time `bson:"field_a"` FieldB *time.Time `bson:"field_b"` } func main() { now := time.Now() test := testStruct{ FieldA: now, FieldB: &now, } data, err := bson.Marshal(test) if err != nil { log.Fatalf("Failed to marshal. Error: %v", err) } unTest := testStruct{} err = bson.Unmarshal(data, &unTest) if err != nil { log.Fatalf("Failed to marshal. Error: %v", err) } log.Printf("Marshaled: %+v", test) log.Printf("Unmarshaled:%+v", unTest) } |
Will give something like this:
2018/05/25 11:24:09 Marshaled: {FieldA:2018-05-25 11:24:09.703404724 +1000 AEST m=+0.001358642 FieldB:2018-05-25 11:24:09.703404724 +1000 AEST m=+0.001358642} 2018/05/25 11:24:09 Unmarshaled:{FieldA:2018-05-25 11:24:09.703 +1000 AEST FieldB:0001-01-01 00:00:00 +0000 UTC}
So, value for FiledB was not encoded.|