dongmijgnnq0118 2018-10-20 16:45
浏览 82
已采纳

无法从MongoDB中读取类型为strfmt.DateTime的time_stamp

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"), "status" : "test", "time_stamp" : { "data" : "2018-10-21T00:33:59.699+08:00" } }

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
", 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)
    }
}

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
", elem)
    }
}
  • 写回答

1条回答 默认 最新

  • duanlan5320 2018-10-20 19:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿