
// 引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
// 数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true,useUnifiedTopology: true})
// 连接成功
.then(() => console.log('数据库连接成功'))
// 连接失败
.catch(err => console.log(err, '数据库连接失败'));
// 创建集合规则
const postSchema = new mongoose.Schema({
//我们给title字段设置为一个对象,类型type为字符串,required必传字段为true.
//意思是必须要有title字段
title:{
type:String,
//必选字段
required:[true,'请输入文章标题'],
//字段最小长度
minlength:[2,'文章长度不能少于2'],
//字段最大长度
maxlength:[5,'文章长度不能多于5'],
//去除字符串两边空格
trim:true
},
age:{
type:Number,
min:18,
max:50
}
});
const Post=mongoose.model('Post', postSchema);
Post.create({title:'aaaa',age:10}).then(result=>console.log(result));

// 引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
// 数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true,useUnifiedTopology: true})
// 连接成功
.then(() => console.log('数据库连接成功'))
// 连接失败
.catch(err => console.log(err, '数据库连接失败'));
// 创建集合规则
const postSchema = new mongoose.Schema({
//我们给title字段设置为一个对象,类型type为字符串,required必传字段为true.
//意思是必须要有title字段
title:{
type:String,
//必选字段
required:[true,'请输入文章标题'],
//字段最小长度
minlength:[2,'文章长度不能少于2'],
//字段最大长度
maxlength:[5,'文章长度不能多于5'],
//去除字符串两边空格
trim:true
},
age:{
type:Number,
min:18,
max:50
}
});
const Post=mongoose.model('Post', postSchema);
Post.create({title:'aaaa',age:40}).then(result=>console.log(result));

// 引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
// 数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true,useUnifiedTopology: true})
// 连接成功
.then(() => console.log('数据库连接成功'))
// 连接失败
.catch(err => console.log(err, '数据库连接失败'));
// 创建集合规则
const postSchema = new mongoose.Schema({
//我们给title字段设置为一个对象,类型type为字符串,required必传字段为true.
//意思是必须要有title字段
title:{
type:String,
//必选字段
required:[true,'请输入文章标题'],
//字段最小长度
minlength:[2,'文章长度不能少于2'],
//字段最大长度
maxlength:[5,'文章长度不能多于5'],
//去除字符串两边空格
trim:true
},
age:{
type:Number,
min:18,
max:50
},
publishDate:{
type:Date,
default:Date.now
}
});
const Post=mongoose.model('Post', postSchema);
Post.create({title:'aaaa',age:40}).then(result=>console.log(result));

可视化工具compass里面有现在时间,

// 引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
// 数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true,useUnifiedTopology: true})
// 连接成功
.then(() => console.log('数据库连接成功'))
// 连接失败
.catch(err => console.log(err, '数据库连接失败'));
// 创建集合规则
const postSchema = new mongoose.Schema({
//我们给title字段设置为一个对象,类型type为字符串,required必传字段为true.
//意思是必须要有title字段
title:{
type:String,
//必选字段
required:[true,'请输入文章标题'],
//字段最小长度
minlength:[2,'文章长度不能少于2'],
//字段最大长度
maxlength:[5,'文章长度不能多于5'],
//去除字符串两边空格
trim:true
},
age:{
type:Number,
min:18,
max:50
},
publishDate:{
type:Date,
default:Date.now
},
category:{
type:String,
enum:['html','javascript','css','node.js']
}
});
const Post=mongoose.model('Post', postSchema);
Post.create({title:'aaaa',age:40,category:'java'}).then(result=>console.log(result));

枚举,列举出当前字段可以拥有的值
// 引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
// 数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true,useUnifiedTopology: true})
// 连接成功
.then(() => console.log('数据库连接成功'))
// 连接失败
.catch(err => console.log(err, '数据库连接失败'));
// 创建集合规则
const postSchema = new mongoose.Schema({
//我们给title字段设置为一个对象,类型type为字符串,required必传字段为true.
//意思是必须要有title字段
title:{
type:String,
//必选字段
required:[true,'请输入文章标题'],
//字段最小长度
minlength:[2,'文章长度不能少于2'],
//字段最大长度
maxlength:[5,'文章长度不能多于5'],
//去除字符串两边空格
trim:true
},
age:{
type:Number,
min:18,
max:50
},
publishDate:{
type:Date,
default:Date.now
},
category:{
type:String,
enum:['html','javascript','css','node.js']
}
});
const Post=mongoose.model('Post', postSchema);
Post.create({title:'aaaa',age:40,category:'html'}).then(result=>console.log(result));


// 引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
// 数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true,useUnifiedTopology: true})
// 连接成功
.then(() => console.log('数据库连接成功'))
// 连接失败
.catch(err => console.log(err, '数据库连接失败'));
// 创建集合规则
const postSchema = new mongoose.Schema({
//我们给title字段设置为一个对象,类型type为字符串,required必传字段为true.
//意思是必须要有title字段
title:{
type:String,
//必选字段
required:[true,'请输入文章标题'],
//字段最小长度
minlength:[2,'文章长度不能少于2'],
//字段最大长度
maxlength:[5,'文章长度不能多于5'],
//去除字符串两边空格
trim:true
},
age:{
type:Number,
min:18,
max:50
},
publishDate:{
type:Date,
default:Date.now
},
category:{
type:String,
enum:['html','javascript','css','node.js']
},
author: {
type: String,
validate: {
validator: v => {
// 返回布尔值
// true 验证成功
// false 验证失败
// v 要验证的值
return v && v.length > 4
},
// 自定义错误信息
message: '传入的值不符合验证规则'
}
}
});
const Post=mongoose.model('Post', postSchema);
Post.create({title:'aaaa',age:40,category:'html',author:'bd'}).then(result=>console.log(result));

本文探讨了MongoDB的Mongoose库中的验证规则,通过举例说明如何设置当前时间戳和枚举类型,确保字段值的正确性和一致性。在可视化工具Compass中,可以直观看到这些规则的应用,同时了解如何定义字段可能的枚举值。
&spm=1001.2101.3001.5002&articleId=115563274&d=1&t=3&u=a3b02a9f344b4fd59eab7e6aa757af36)
900

被折叠的 条评论
为什么被折叠?



