dourao3960 2018-04-04 17:52
浏览 338
已采纳

在mongodb记录中使用time.Time

I'm inserting new item in collection. Using official mongo go driver for this (https://github.com/mongodb/mongo-go-driver).

collection.InsertOne(context.Background(), map[string]interface{}{
    "string":   "test",
    "integer":  123,
    "float":    0.123,
    "array":    []string{"t", "e", "s", "t"},
    "objectid": objectid.New(),
    "time":     time.Now(),
})

But as a result I have a problem with couple of properties: time.Time and objectid.ObjectID.

  • time.Time is going as Object that is empty
  • objectid.ObjectID - as Binary

enter image description here

I understand that it's only in alpha state, but maybe someone knows. Am I just doing it wrong or it's not yet implemented in the way as it should be?

  • 写回答

2条回答 默认 最新

  • doudao1282 2018-04-05 10:08
    关注

    If you pass a map as the document to Collection.InsertOne(), the mongo package will use the mongo.TransformDocument() to convert it to a *bson.Document value, because most operations are only implemented on bson.Documents.

    The current transformation implementation does not handle objectid.ObjectID nor the time.Time types. It could and probably should, and I assume it will, but currently it doesn't.

    If you want these types to end up with proper types in MongoDB, you may construct and pass a *bson.Document yourself, in which you can explicitly dictate what the types of the properties should be.

    This is an equivalent insert statement to yours, using bson.NewDocument() to create the document manually:

    res, err := coll.InsertOne(context.Background(), bson.NewDocument(
        bson.EC.String("string", "test"),
        bson.EC.Int64("integer", 123),
        bson.EC.Double("float", 0.123),
        bson.EC.ArrayFromElements("array",
            bson.VC.String("t"), bson.VC.String("e"),
            bson.VC.String("s"), bson.VC.String("t")),
        bson.EC.ObjectID("objectid", objectid.New()),
        bson.EC.DateTime("time", time.Now().UnixNano()/1e6), // Must pass milliseconds
    ))
    

    It's more verbose, but it's explicit in what we want the result document in MongoDB to be. The result document will look like this:

    {
        "_id" : ObjectId("5ac5f598ca151255c6fc0ffb"),
        "string" : "test",
        "integer" : NumberLong(123),
        "float" : 0.123,
        "array" : [
            "t",
            "e",
            "s",
            "t"
        ],
        "objectid" : ObjectId("5ac5f598ca151255c6fc0ffa"),
        "time" : ISODate("2018-04-05T10:08:24.148Z")
    }
    

    Once the driver improves, I assume your original version will work as expected and produce a document identical to this in structure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题