12-2
调整:
// 引入:
const mongoose = require("mongoose");//1.安装mongoose
mongoose.connect("mongodb://127.0.0.1:27017/test")//2.建立连接
// 创建一个模型 tagSchema
const tagSchema = new mongoose.Schema({
//text: String,
text: {
type: String,
minlength: 2,
maxlength: 12
}//3.约束
})
const tagModel = new mongoose.model("tag", tagSchema);
// tagMOdel.
// module.exports = userModel;
// 调用函数
// const arr = [{ text: "HTML" }, { text: "SQL" }, { text: "NOdeJS" }]
// tagModel.insertMany(arr, function (err, docs) {
// console.log(err);
// console.log(docs);
// });
// 创建第二个页面 userSchema
const userSchema = new mongoose.Schema({
title: {
type: String,
minlength: 6,
maxlength: 12
},
content: {
type: String,
minlength: 10,
maxlength: 50
},
top: {
type: Boolean,
default:false
}
})
const userModel = new mongoose.model("user", userSchema);
// 第三个页面contentSchema
const contentSchema = new mongoose.Schema({
name: {
type: String,
minlength: [2, "用户名最少2个字符"],
maxlength: [12, "用户名最多12个字符"]
},
password: {
type: String,
validate: function (v) { // 验证
return /[a-zA-Z0-9_]{6,12}/.test(v);
},
message: "密码只能是6-12的数字、字母和下划线的任意组合"
},
email: {
type: String,
validate: {
validator: function (v) {
return /\w+@\w+\.\w+/.test(v);
},
message: "邮箱格式不正确"
},
},
mark: {
type: String,
enum: {
values: ['限制会员', '新手上路', '组册会员', '中级会员', '高级会员'],//枚举
message: '{VALUE} is not supported'
}
}
})
const contentModel = new mongoose.model("content", contentSchema);
// 导出:
module.exports = { tagModel, userModel, contentModel };
修改:
简易写法:
第一个页面调整:
### 查询内容
GET {{url}}/User
### 添加内容
POST {{url}}/User
{{json}}
{
"title":"titlyg",
"content":"congitiugu"
}
### 删除
DELETE http://localhost:3000/tag HTTP/1.1
Content-Type: application/json
{
"title":"卢本伟niubi"
}
### 修改内容
PUT {{url}}/user?_id=61a8836307582e0630dee75f
{{json}}
{
"title":"卢本伟niubi"
}
效果:
