登录
首页 >  Golang >  Go问答

Go 中的 mongodb 驱动程序与其他语言和其他数据库中的比较的时间精度问题

来源:stackoverflow

时间:2024-04-20 09:15:34 270浏览 收藏

小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《Go 中的 mongodb 驱动程序与其他语言和其他数据库中的比较的时间精度问题》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!

问题内容

我正在学习 go 和 mongodb,目前使用 alpha 官方 mongodb 驱动程序。虽然它还处于 alpha 阶段,但我认为它对于基本用途来说已经相当实用了。 但我在这个数据库驱动程序中遇到了一个关于时间转换的有趣问题。

基本上,我创建了一个自定义类型的结构对象,并将其封送到 bson 文档,然后将 bson 文档转换回结构对象。

//check github.com/mongodb/mongo-go-driver/blob/master/bson/marshal_test.go
func testuserstructtobsonandbackwards(t *testing.t) {
u := user{
    username:          "test_bson_username",
    password:          "1234",
    useraccessibility: "normal",
    registerationtime: time.now(), //.format(time.rfc3339), adding format result a string
}

//struct to bson
bsonbytearray, err := bson.marshal(u)
if err != nil {
    t.error(err)
}
//.unmarshaldocument is the same as readdocument
bdoc, err := bson.unmarshaldocument(bsonbytearray)
if err != nil {
    t.error(err)
}
unamefrombson, err := bdoc.lookuperr("username")
//so here the binding is working for bson object too, the bind field named username ratherthan username
if err != nil {
    t.error(err)
}
if unamefrombson.stringvalue() != "test_bson_username" {
    t.error("bson from user struct error")
}

//bson doc to user struct
bsonbytearrayfromdoc, err := bdoc.marshalbson()
if err != nil {
    t.error(err)
}

var newu user
err = bson.unmarshal(bsonbytearrayfromdoc, &newu)
if err != nil {
    t.error(err)
}

if newu.username != u.username {
    t.error("bson doc to user struct error")
}

//here we have an issue about time format.
if newu != u {
    log.println(newu)
    log.println(u)
    t.error("bson doc to user struct time error")
}
}

但是,由于我的结构对象有一个时间字段,因此结果结构对象包含的时间值不如原始值准确。那么比较失败。

=== RUN   TestUserStructToBsonAndBackwards
{test_bson_username 1234     0001-01-01 00:00:00 +0000 UTC   2018-08-28 23:56:50.006 +0800 CST 0001-01-01 00:00:00 +0000 UTC normal }
{test_bson_username 1234     0001-01-01 00:00:00 +0000 UTC   2018-08-28 23:56:50.006395949 +0800 CST m=+0.111119920 0001-01-01 00:00:00 +0000 UTC normal }
--- FAIL: TestUserStructToBsonAndBackwards (0.00s)
    model.user_test.go:67: bson Doc to user struct time Error

所以我想问很多问题。

  1. 在这种情况下如何正确比较时间?

  2. 在数据库中存储时间以避免此类精度问题的最佳方法是什么?我认为数据库中的时间不应该是字符串。

  3. 这是数据库驱动程序错误吗?


解决方案


bson 中的时间表示为自 unix 纪元 (spec) 以来的 utc 毫秒。 go 中的时间值具有纳秒精度。

对于往返时间。通过 bson 编组的时间值,使用自 unix 纪元以来截断为毫秒的时间:

func truncate(t time.time) time.time {
    return time.unix(0, t.unixnano()/1e6*1e6)
}

...

u := user{
    username:          "test_bson_username",
    password:          "1234",
    useraccessibility: "normal",
    registerationtime: truncate(time.now()), 
}

您还可以使用Time.Truncate方法:

u := user{
    username:          "test_bson_username",
    password:          "1234",
    useraccessibility: "normal",
    registerationtime:  time.now().truncate(time.millisecond),
}

此方法依赖于 unix 纪元和 go 零时间相差整数毫秒这一事实。

您已正确识别出问题是精度之一.

MongoDB's Date type is“一个 64 位整数,表示毫秒数...”。

Golang's time.Time type“表示纳秒精度的瞬间”。

因此,如果将这些各自的值作为 golang 类型进行比较,只有当 golang 时间具有毫秒分辨率(例如,微秒和纳秒位置为零)时,您才会获得等效值。

例如:

gotime := time.Now() // Nanosecond precision
jstime := gotime.Truncate(time.Millisecond) // Milliseconds
gotime == jstime // => likely false (different precision)

isoMillis := "2006-01-02T15:04:05.000-0700Z"
gomillis := gotime.Format(isoMillis)
jsmillis := jstime.Format(isoMillis)
gomillis == jsmillis // => true (same precision)

今天关于《Go 中的 mongodb 驱动程序与其他语言和其他数据库中的比较的时间精度问题》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>