//连接数据库
const mongoose = require('mongoose');
//开始连接数据库
mongoose.connect('mongodb://localhost/students')
.then(() => console.log('连接数据库成功!'))
.catch(err => {
console.log('连接失败!' + err)
})
//创建数据库用户集合规则
const userSchema = new mongoose.Schema({
name: {
type: String,
required: [true, '名称不能为空'], //required:true, //必传
minlength: [2, '标题最小值为2,长度不够'], //规定传入的字符串的最小长度
maxlength: [10, '最大值为10'], //规定传入的字符串的最大长度
trim: true, //去除字符串两边的空格
},
age: { //年龄
type: Number,
min: [10, '最小值为10'],
max: [28, '最大值为28']
},
sex: { //性别
type: String,
enum: { //分类只能填写enum中的内容
values: ['男', '女', ],
message: '要么男要么女'
}
},
email: String, //邮箱
hobbies: [String], //兴趣爱好
collage: String, //所属学院
create_time: { //入学日期
type: Date,
default: Date.now //数据添加时调用当前时间
},
date: { //入学日期
type: Date,
default: Date.now //数据添加时调用当前时间
}
})
const User = mongoose.model('Users', userSchema) //sourses
//公共内容开放出去
module.exports = User;
版权属于:
再度Miren
评论 (0)