我想在MongoDB中保存时间,以便为学校添加教师的可用性。如何将一天的时间存储在MongoDB中,作为一个字符串呢?(我想存储如下格式:03:30 PM)请帮我找到解决方案。
我想在MongoDB中保存时间,以便为学校添加教师的可用性。如何将一天的时间存储在MongoDB中,作为一个字符串呢?(我想存储如下格式:03:30 PM)请帮我找到解决方案。
您可以使用Mongoose用户模式的pre函数,每次使用该模式添加任何内容时都会触发。在其中使用条件函数来确定如何保存特定的模式模型。我已经完成了您的时间模型。如果有任何疑问,请告诉我。
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var UserSchema = new Schema({
time: String
});
UserSchema.pre('save',
function (next) {
if (!this.time) {
var newdate = new Date();
var Fulllocaltime = newdate.toLocaleString('en-US');
var array = Fulllocaltime.split(" ");
var finaloutput = array[1]+' '+array[2];
this.time =finaloutput;
}
next();
}
);