Node开发--->11_Node.js_mongoDB验证规则
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Node开发--->11_Node.js_mongoDB验证规则,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4680字,纯文字阅读大概需要7分钟。
内容图文
2.6 mongoose验证
在创建集合的时候,可以为当前集合当中的字段去设置一些格式上的验证规则。比如:当前字段的类型是字符串类型,我们可以设置字段在存入值的时候,限定字符串的最大和最小长度,当验证成功的时候,当前的字段就可以插入集合。
验证规则之required:true必传字段
在创建集合的时候,规定了集合可以拥有哪些字段,默认情况下这些字段都是可选的
//引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
//数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true })
.then(() => console.log('数据库连接成功'))
.catch(err => console.log(err, '数据库连接失败'));
//创建集合规则
const postScheam = new mongoose.Schema({
// title: String,是曾经的写法,如果这样写就指定不了验证规则
//现在修改写法如下:
title: {
type: String,
//现在如果不传title这个字段就会报错
required: true
}
});
//利用规则创建集合
const Post = mongoose.model('Post', postScheam);
//传一个空对象,不插入type字段,那么就会报错
//因为集合规则中有限制:如果不传type,就不会插入成功
Post.create({}).then(res => console.log(res))
运行后
同时我们还可以自定义更加友好的报错信息
//引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
//数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true })
.then(() => console.log('数据库连接成功'))
.catch(err => console.log(err, '数据库连接失败'));
//创建集合规则
const postScheam = new mongoose.Schema({
// title: String,是曾经的写法,如果这样写就指定不了验证规则
//现在修改写法如下:
title: {
type: String,
//定义更加优化的报错信息
required: [true, '请传入文章标题']
}
});
//利用规则创建集合
const Post = mongoose.model('Post', postScheam);
Post.create({}).then(res => console.log(res))
运行后
验证规则之minlength,maxlength
针对于字符串,限定字段的最大最小长度
//引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
//数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true })
.then(() => console.log('数据库连接成功'))
.catch(err => console.log(err, '数据库连接失败'));
//创建集合规则
const postScheam = new mongoose.Schema({
title: {
type: String,
required: [true, '请传入文章标题'],
//限定title的最下和最大长度的
minlength: 2,
maxlength: 5
}
});
//利用规则创建集合
const Post = mongoose.model('Post', postScheam);
//传入的长度与规定的长度不符合,也报错
Post.create({ title: 'a' }).then(res => console.log(res))
运行后
minlength和maxlength都可以自定义错误信息,形式同require
验证规则之trim
去除字符串两边的空格
//引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
//数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true })
.then(() => console.log('数据库连接成功'))
.catch(err => console.log(err, '数据库连接失败'));
//创建集合规则
const postScheam = new mongoose.Schema({
// title: String,是曾经的写法,如果这样写就指定不了验证规则
//现在修改写法如下:
title: {
type: String,
required: [true, '请传入文章标题'],
minlength: 2,
maxlength: 5,
//去除字符串两边的空格
trim: true
}
});
//利用规则创建集合
const Post = mongoose.model('Post', postScheam);
//trim去除字符串两边的空格 插入到数据库后左右两边无空格
Post.create({ title: ' aa ' }).then(res => console.log(res))
验证规则之min,max
针对数值字段类型
//引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
//数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true })
.then(() => console.log('数据库连接成功'))
.catch(err => console.log(err, '数据库连接失败'));
//创建集合规则
const postScheam = new mongoose.Schema({:
title: {
type: String,
required: [true, '请传入文章标题'],
minlength: 2,
maxlength: 5,
trim: true
},
age: {
type: Number,
min: 18,
max: 100
}
});
//利用规则创建集合
const Post = mongoose.model('Post', postScheam);
//年龄小于18 报错
Post.create({ title: 'aa', age: 10 }).then(res => console.log(res))
验证规则之default
对于非可选字段,当插入数据的时候,不为该字段赋值,那么就会自动有一个默认值
//引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
//数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true })
.then(() => console.log('数据库连接成功'))
.catch(err => console.log(err, '数据库连接失败'));
//创建集合规则
const postScheam = new mongoose.Schema({
// title: String,是曾经的写法,如果这样写就指定不了验证规则
//现在修改写法如下:
title: {
type: String,
required: [true, '请传入文章标题'],
minlength: 2,
maxlength: 5,
trim: true
},
age: {
type: Number,
min: 18,
max: 100
},
publishDate: {
//该字段的类型是时间
//不为该字段赋值的时候,默认传递当前的时间
type: Date,
default: Date.now //默认传递的时间
}
});
//利用规则创建集合
const Post = mongoose.model('Post', postScheam);
//不传递publishDate
Post.create({ title: 'aa', age: 60 }).then(res => console.log(res))
运行后
打开campass
内容总结
以上是互联网集市为您收集整理的Node开发--->11_Node.js_mongoDB验证规则全部内容,希望文章能够帮你解决Node开发--->11_Node.js_mongoDB验证规则所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。