我正在尝试将strfmt.DateTime类型为(https://godoc.org/github.com/go-openapi/strfmt#DateTime)的时间戳写入 mongodb我可以成功地将这种格式的日期写入数据库,如下所示:{ “_id”:ObjectId(“5bcb58f7540ac6d0bc946e22”), “状态”:“测试”, “time_stamp”:{ “数据”:“2018-10-21T00:33:59.699+08:00” } }但我无法从 mongodb 中检索它,time_stamp 的值始终显示0001-01-01T00:00:00.000Z,我只是不明白为什么。这是我的代码,欢迎任何建议或意见!谢谢package mainimport ( "fmt" "time" "github.com/go-openapi/strfmt" "github.com/op/go-logging" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson")type TxStatus struct { Status string `json:"status" bson:"status"` TimeStamp *strfmt.DateTime `json:"time_stamp" bson:"time_stamp"`}type MongoDBOperations struct { mongoSession *mgo.Session database string collection string}var log = logging.MustGetLogger("example")func main() { mo := MongoDBOperations{} mo.database = Database mo.collection = Collection // We need this object to establish a session to our MongoDB. mongoDBDialInfo := &mgo.DialInfo{ Addrs: []string{MongoDBHosts}, Timeout: 60 * time.Second, Database: AuthDatabase, Username: AuthUserName, Password: AuthPassword, } // Create a session which maintains a pool of socket connections // to our MongoDB. var err error mo.mongoSession, err = mgo.DialWithInfo(mongoDBDialInfo) if err != nil { log.Fatalf("CreateSession: %s\n", err) } mo.mongoSession.SetMode(mgo.Eventual, true) write(mo) read(mo)}func write(mo MongoDBOperations) { log.Info("write operation") session := mo.mongoSession.Copy() defer session.Close() c := session.DB(Database).C(Collection) timestamp := strfmt.DateTime(time.Now()) txStatus := TxStatus{ Status: "test", TimeStamp: ×tamp, } if err2 := c.Insert(txStatus); err2 != nil { panic(err2) }
1 回答
肥皂起泡泡
TA贡献1829条经验 获得超6个赞
我仔细研究了 的代码github.com/go-openapi/strfmt
,发现他们使用的github.com/globalsign/mgo
是gopkg.in/mgo.v2
. 将导入更改为使用github.com/globalsign/mgo
并github.com/globalsign/mgo/bson
解决了这个问题。Globalsign 包是 fork 的mgo.v2
,所以方法保持不变,除了导入之外不需要更改任何代码。
- 1 回答
- 0 关注
- 100 浏览
添加回答
举报
0/150
提交
取消