无法从MongoDB中读取类型为`strfmt.DateTime`的time_stamp [英] Cannot read time_stamp with type `strfmt.DateTime` from MongoDB
问题描述
我正在尝试使用strfmt.DateTime
类型( https: //godoc.org/github.com/go-openapi/strfmt#DateTime )进入mongodb
I'm trying to write timestamp with strfmt.DateTime
type (https://godoc.org/github.com/go-openapi/strfmt#DateTime) into the mongodb
我可以成功地将这种日期格式写入数据库,如下所示:
I can successfully write this format of date into the DB, which looks like this:
{"_id":ObjectId("5bcb58f7540ac6d0bc946e22"),状态":测试",时间标记":{ "data":"2018-10-21T00:33:59.699 + 08:00"}}
{ "_id" : ObjectId("5bcb58f7540ac6d0bc946e22"), "status" : "test", "time_stamp" : { "data" : "2018-10-21T00:33:59.699+08:00" } }
但是我只是无法从mongodb中检索它,time_stamp的值始终显示0001-01-01T00:00:00.000Z
,我只是不明白为什么.
But I just can't retrieve it from the mongodb, the value of the time_stamp always shows 0001-01-01T00:00:00.000Z
, I just don't see why.
这是我的代码,欢迎任何建议或意见!谢谢
Here is my code, any suggestions or opinions are welcome! Thanks
package main
import (
"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)
}
}
func read(mo MongoDBOperations) {
log.Info("write operation")
session := mo.mongoSession.Copy()
defer session.Close()
c := session.DB(Database).C(Collection)
// Find and Count
var status []TxStatus
err2 := c.Find(bson.M{"status": "test"}).All(&status)
if err2 != nil {
panic(err2)
}
for _, elem := range status {
fmt.Printf("%+v\n", elem)
}
}
推荐答案
我仔细研究了github.com/go-openapi/strfmt
的代码,发现我发现他们使用的是github.com/globalsign/mgo
而不是gopkg.in/mgo.v2
.将导入更改为使用github.com/globalsign/mgo
和github.com/globalsign/mgo/bson
可以解决此问题. Globalsign包是mgo.v2
的fork,因此方法保持不变,除了import之外无需更改任何代码.
I dug through the code of github.com/go-openapi/strfmt
and what I've found out is that they are using github.com/globalsign/mgo
instead of gopkg.in/mgo.v2
. Changing imports to use github.com/globalsign/mgo
and github.com/globalsign/mgo/bson
has fixed the issue. Globalsign package is fork of mgo.v2
so the methods remain the same and there is no need to change any code besides import.
这篇关于无法从MongoDB中读取类型为`strfmt.DateTime`的time_stamp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!