当前位置: 首页 > 面试题库 >

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

经兴安
2023-03-14
问题内容

我正在尝试将strfmt.DateTime类型(https://godoc.org/github.com/go-
openapi/strfmt#DateTime
)的时间戳写入mongodb

我可以成功地将这种日期格式写入数据库,如下所示:

{“ _id”:ObjectId(“ 5bcb58f7540ac6d0bc946e22”),“状态”:“测试”,“时间标记”:{“数据”:“
2018-10-21T00:33:59.699 + 08:00”}}

但是我只是无法从mongodb中检索它,time_stamp的值始终显示0001-01-01T00:00:00.000Z,我只是不明白为什么。

这是我的代码,欢迎任何建议或意见!谢谢

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: &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\n", elem)
    }
}

问题答案:

我仔细研究了的代码,github.com/go- openapi/strfmt发现它们正在使用github.com/globalsign/mgo而不是gopkg.in/mgo.v2。更改进口使用方式github.com/globalsign/mgogithub.com/globalsign/mgo/bson解决了该问题。Globalsign包是其中的一个分支,mgo.v2因此方法保持不变,除了导入外,无需更改任何代码。



 类似资料:
  • 我有一个3节点的Apache Ignite集群,我创建了一个以整数为键的缓存,以'subscriber'POJO为值,当我从JAVA程序内部连接到集群并访问缓存时,我得到了上面提到的异常,我有'peerClassLoading'属性设置为false,并且我在所有节点中部署了'subscriber'POJO二进制文件,请在下面找到完整的堆栈跟踪。我错过了什么?当我在JAVA程序中用启动客户端时,它为

  • 我有使用JPA/Hibernate、MySQL的Spring boot项目。我有三个多对多关系的道类。 Poko类如下所示 产品 ProductExtra公司 额外的 包含查询的额外存储库 控制器中的映射 我试图进行多对多的查询来选择每个产品中的附加功能,但是我得到了这个错误错误消息令人惊讶地也包含我想要的结果。不确定我做错了什么

  • 我有一个问题做npm启动,这似乎是一个bug与css-loader但我不能修复它。 我读过这篇文章,但我不能让它工作:https://github.com/reactjsresources/react-webpack-babel/issues/197 我还修改了webpack.config.js,添加了: 和: 如果有人能帮忙请... 谢谢.

  • 问题内容: 此Firebase数据库结构: 在此处输入图片说明 这是我的设备类: } 第二类Counters Class我的问题是我不知道如何使用和从数据库中读取对象。 } 这是我的HomeActivity从Firebase对象读取的内容 这是我的错误代码,请 在此处输入图片描述 问题答案: 您收到以下错误: 无法将类型java.util.ArrayList的对象转换为com.example.sm

  • 我的代码: 错误: 未处理PromisejectionWarning:TypeError:无法读取未定义的未处理PromisejectionWarning:未处理的promise拒绝的属性“forEach”。此错误源于在没有catch块的情况下抛出异步函数的内部,或者拒绝使用未处理的promise。catch()。(拒绝id:1)(节点:7188)[DEP0018]弃用警告:未处理的promise

  • TypeError:无法读取app.post(/var/www/html/express/simplecrud/server.js:20:21)层未定义的属性“collection”。处理[as handle_request](/var/www/html/express/simplecrud/node_modules/express/lib/router/layer.js:95:5)层的下一个(/