亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法從 MongoDB 讀取類型為“strfmt.DateTime”的時間戳

無法從 MongoDB 讀取類型為“strfmt.DateTime”的時間戳

Go
至尊寶的傳說 2023-04-24 16:42:43
我正在嘗試將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: &timestamp,    }    if err2 := c.Insert(txStatus); err2 != nil {        panic(err2)    }
查看完整描述

1 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

我仔細研究了 的代碼github.com/go-openapi/strfmt,發現他們使用的github.com/globalsign/mgogopkg.in/mgo.v2. 將導入更改為使用github.com/globalsign/mgogithub.com/globalsign/mgo/bson解決了這個問題。Globalsign 包是 fork 的mgo.v2,所以方法保持不變,除了導入之外不需要更改任何代碼。



查看完整回答
反對 回復 2023-04-24
  • 1 回答
  • 0 關注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號