【问题标题】:How do you save Timezone inside MongoDB如何在 MongoDB 中保存时区
【发布时间】:2022-01-05 13:54:58
【问题描述】:

我想在 MongoDB 中创建一个模式,我可以在其中将时区保存到 MongoDB 数据库中。 我有这个作为架构

'use strict';

var mongoose = require('mongoose'),
    bcrypt = require('bcrypt'),
    Schema = mongoose.Schema;


var UserSchema  = new Schema({
    fullname :{
        type:String,
        trim:true,
        required:true
    },
    email:{
        type:String,
        unique:true,
        lowercase:true,
        trim:true,
        required:true
    },
    hash_password:{
        type:String
    },
    timezone:{
        type:String,
        
    },
    created:{
        type: Date,
        default: Date.now
    }
});

UserSchema.methods.comparePassword = function(password){
    return bcrypt.compareSync(password,this.hash_password);
};

mongoose.model('User', UserSchema);

假设我想将时区保存在数据库中。我如何将它也包含在架构中?我只是将其保存为纯字符串吗?或者究竟是什么?

【问题讨论】:

  • 您确定需要吗?在许多情况下,存储没有时区的时间戳并将其强制为 Zulu 时间会使事情变得更简单,因为您可以使用一个时间来进行比较和执行定期操作。

标签: mongodb timezone


【解决方案1】:

将时区存储为字符串似乎是合理的。

将时区存储为小时/分钟 UTC 偏移量缺少夏令时设置。确保使用IANA Timezone database 名称(即没有像EST 这样的时区名称)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多