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 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目