Mongodb Mongo Db时间格式
我想在mongoDB中节省时间,以便增加学校的教师可用性。如何在mongoDB中以字符串形式存储一天中的时间。 (我想在下午3:30左右买)Mongodb Mongo Db时间格式,mongodb,Mongodb,我想在mongoDB中节省时间,以便增加学校的教师可用性。如何在mongoDB中以字符串形式存储一天中的时间。 (我想在下午3:30左右买) 请帮助我找到解决方案?您可以使用mongoose user schema的pre函数,每当您使用该模式添加任何内容时,该函数都会触发。在该函数中,使用条件函数来保存特定的模式模型。我已经做了你的时间模型。如果您有任何疑问,请告诉我 var mongoose = require('mongoose'), Schema = mongoose.Schema;
请帮助我找到解决方案?您可以使用mongoose user schema的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();
}
);
为什么不直接存储为从一天开始算起的毫秒数呢?它比字符串占用更少的空间,并且具有更广泛的用途。