英文:
How I insert a time-date-stamp in MongoDB with a Golang Sruct?
问题
我有以下的结构体:
type TypeIncidence struct {
Number int `bson:"number" json:"number"`
Description string `bson:"description" json:"description"`
Date_time_stamp string `bson:"dateTimeStamp" json:"date_time_stamp"`
}
我想要在一个集合中插入一个文档:
type TypeIncidence struct {
Number int `bson:"number" json:"number"`
Description string `bson:"description" json:"description"`
Date_time_stamp **string?**
}
var incidence TypeIncidence
incidence.Number = 1
Description = "Text"
Date_time_stamp = **string?**
在Golang结构体中,要使用什么数据类型来存储date_time_stamp
作为字符串?
如果我想要以以下格式存储:'YYYY-MM-DD hh:mm:ss',在Golang中应该使用哪个模块和/或函数?(在本地机器或服务器上转换时区时间)
提前感谢。
英文:
I have the following struct:
type TypeIncidence struct {
Number int bson:"number" json:"number"
Description string bson:"description" json:"description"
Date_time_stamp string bson:"dateTimeStamp" json:"date_time_stamp"
}
and I want insert a document in a collection:
type TypeIncidence struct {
Number int `bson:"number" json:"number"`
Description string `bson:"description" json:"description"`
Date_time_stamp **string?**
}
var incidence TypeIncidence
incidence.Number = 1
Description =" Text"
Date_time_stamp = **string?**
What data type would I have to use in a Golang structure to store date_time_struct a string?
If I want to store with the following format 'YYYY-MM-DD hh:mm:ss', what module and/or function should I use in golang? (in local machine or server converting zone time)
Thanks in advance
答案1
得分: 3
你可以使用time.Time
:
CreatedAt time.Time `json:"created_at" bson:"created_at"`
然而,我建议你存储Epoch Unix时间戳(自1970年1月1日以来的秒数),因为它是通用的:
CreatedAt int64 `json:"created_at" bson:"created_at"`
过去我尝试过在Golang中将time.Time
存储在MongoDB中,但当我将相同的信息解析为Python中的datetime
对象时遇到了问题。如果你希望在不同的语言和技术之间兼容,存储Epoch Unix时间戳将是一个很好的选择。
英文:
You can use time.Time
:
CreatedAt time.Time `json:"created_at" bson:"created_at"`
However, I would recommend that you store Epoch Unix timestamp (the number of seconds since Jan 1st 1970) because it is universal:
CreatedAt int64 `json:"created_at" bson:"created_at"`
I have tried in the past to store time.Time
in MongoDB through Golang but then I had trouble when I parsed the same information into a datetime
object in Python. If you would like to be compatible across languages and technologies, storing the Epoch Unix timestamp would be a great option.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论