doushangxianq07480 2016-06-23 11:36
浏览 813

Golang / mgo:如何在mongodb中存储日期(不是ISODate)?

If I store current time like this:

type Test struct {
    Id string `bson:"id" json:"id,omitempty"`
    TestTime time.Time `bson:"testTime" json:"testTime,omitempty"`
}
...

t := Test {
    Id : "TEST0001",
    TestTime : time.Now(),
}
...

c.Insert(t)

Then I use mongochef to search it :

{ 
    "_id" : ObjectId("576bc7a48114a14b47920d60"), 
    "id" : "TEST0001", 
    "testTime" : ISODate("2016-06-23T11:27:30.447+0000")
}

So, mgo store ISODate by default, how can I store Date not ISODate ?

  • 写回答

2条回答 默认 最新

  • duanchoupo1104 2016-06-23 11:58
    关注

    mgo automagically converts time.Time into a Mongo internal date data type (source, actually it's just a timestamp with no timezone info and it's always corrected to UTC). Any other functionaly has to be manually implemented by you.

    You can force mgo to correctly (de)serialize your types by implementing the Getter and Setter interfaces from package mgo/bson thought it's pretty low-level so be careful.

    评论

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员