使用mongoose存储时间的数据类型

data type to store time with mongoose

本文关键字:数据类型 时间 存储 mongoose 使用      更新时间:2023-09-26

我必须使用MEAN堆栈为医生制定预约系统。在我的模式中,我必须存储插槽的开始时间和结束时间。Mongoose没有存储时间的数据类型。我应该使用什么数据类型来存储时间?

Mongoose的Date模式类型不仅表示日期,还表示完整的日期和时间戳,因此这将是合乎逻辑的选择。

var slotSchema = new Schema({
    startTime: Date,
    endTime: Date,
    ...
});

您可以在定义Schema时将timestamps属性设置为true,mongoose将为您添加createdAtupdatedAt字段。当您执行任何更新操作时,Mongoose还会为您更新updatedAt字段。

var schema = new Schema({
    // ... Schema properties
}, {
    timestamps: true
});

http://mongoosejs.com/docs/guide.html#timestamps

Date对象存储一个特定的时刻,精确到毫秒。

请参阅:http://www.robertprice.co.uk/robblog/2011/05/javascript_date_time_and_node_js-shtml/

有关猫鼬的更多信息:http://mongoosejs.com/docs/guide.html