網站首頁 編程語言 正文

1.首先 安裝 mongoose 插件 創建文件夾
npm i mongoose
db文件是咱們連接數據庫的文件
let mongoose = require("mongoose")
mongoose.connect('mongodb://127.0.0.1:27017/exam') // exam是咱們的數據庫名稱
const conn = mongoose.connection
conn.on("open",()=>{
console.log("數據庫連接成功");
})
conn.on("error",err=>{
console.log("失敗",err);
})
module.exports = mongoose //暴露
model文件是咱們創建的表
const mongoose = require("./db")
let Schema = mongoose.Schema
//增加了對數據的校驗功能
let studentSchema = new Schema({
name : {
type : String, //name:String
required : true, //不能為空
maxlength : 10 //最大長度
},
sex : {
type : String,
default : "男" // 默認值
},
time : {
type : Date ,
default : Date.now() //默認值為當前系統時間
},
age : {
type : Number ,
max : 150 , //最大值和最小值 限制
min : 18
},
tel : {
type : String,
match : /^1[358]\d{9}$/ // 手機號的校驗
}
},{
versionKey:false //去掉自動生成的_v字段
})
//第一個參數和第三個參數最好寫成一樣的
let studentModel = mongoose.model( "student" , studentSchema , "student" )
//將所有的Model暴露出去
module.exports = {
studentModel
}
原文鏈接:https://blog.csdn.net/m0_64544033/article/details/129719222
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2024-03-25 SpringBoot項目的基本依賴以及設置
- 2022-09-08 Go語言怎么使用變長參數函數_Golang
- 2022-06-08 Golang原生rpc(rpc服務端源碼解讀)_Golang
- 2022-03-15 C#實現時間戳與標準時間的互轉_C#教程
- 2024-03-23 如何保證Redis和數據庫數據一致性
- 2024-04-06 jeecg-boot使用QueryGenerator.initQueryWrapper怎么排序查詢
- 2022-05-03 詳解Python利用APScheduler框架實現定時任務_python
- 2022-04-06 Go?字符串比較的實現示例_Golang
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支