时区中的MongoDB日期 [英] MongoDB date in timezone
问题描述
有没有办法将日期存储在本地时区而不是 UTC 中?
Is there a way to store Dates in local timezone and not in UTC ?
这是我的架构:
var WhispSchema = new mongoose.Schema({
text : String,
pos : {latitude: Number, longitude: Number},
distance : {type : Number, default : 0},
created_by : {type : Schema.Types.ObjectId, ref : "UserSchema"},
upvote : {type : Number, default : 0},
downvote : {type : Number, default : 0}
});
WhispSchema.plugin(timestamps, {
createdAt: 'created_at',
updatedAt: 'updated_at'
});
但是created_at"和updated_at"字段采用 UTC 格式,我想要本地时区.
But the field "created_at" and "updated_at" is in UTC format and I want the local timezone.
推荐答案
不,BSON 日期 在 MongoDB 中用于存储日期的数据类型被定义为 UTC 日期时间.
No, the BSON Date data type that's used within MongoDB to store dates is defined as a UTC datetime.
但这确实是最好的,因为在其他时区存储日期时间会变得非常混乱.从数据库中获取后,最好根据需要将存储的UTC时间戳转换为本地时区.
But this is really for the best, as storing datetimes in other time zones can get really messy. It's better to convert the stored UTC timestamps to a local timezone as needed after getting it from the database.
参见 这个问题是关于如何在 aggregate
管道中做到这一点的.
See this question regarding ways to do this within an aggregate
pipeline.
这篇关于时区中的MongoDB日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!