在mongoose中为模式类型date赋值,可以通过以下步骤进行操作:
npm install mongoose
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
birthDate: Date
});
const User = mongoose.model('User', userSchema);
const user = new User({
birthDate: new Date()
});
const user = new User({
birthDate: new Date('1990-01-01')
});
const user = new User({
birthDate: new Date('1990-01-01T10:30:00')
});
const user = new User({
birthDate: new Date(1990, 0, 1, 10, 30, 0)
});
user.save()
.then(() => {
console.log('User saved successfully');
})
.catch((error) => {
console.error('Error saving user:', error);
});
以上就是在mongoose中为模式类型date赋值的步骤。需要注意的是,mongoose会自动将JavaScript的Date对象转换为适当的日期格式存储在数据库中。
领取专属 10元无门槛券
手把手带您无忧上云