built-in validation using MongoDB
this information copyright by mongoose offical website only education purpose
Schema Types
Indeses
String
Number
Date
ObjectId
All Schema Types
required | default |
select | validate |
get | set |
alias | immutable |
transform |
Indeses
index | unique |
sparse |
String
lowercase | uppercase |
trim | match |
enum | populate |
minlength | maxlength |
Number
min | max |
enum | populate |
Date
min | max |
ObjectId
populate |
use Validationconst mongoose = require('mongoose');mongoose.connect("mongodb://localhost:27017/newDB",{useNewUrlParser: true}).then(()=>{console.log("connction successful...")}).catch((err)=>console.log(err));const playlistSchema = new mongoose.Schema ({// define schemaname:{type : String,required : true},sirname : String,email : String,role : String,active : Boolean,date : {type: Date,default : Date.now}})// collection creationconst PlayList = new mongoose.model("PlayList",playlistSchema)// create document or insertconst createDocument = async ()=>{try{const FirstStudent = new PlayList({name:"Shreyash",sirname : "KOlhe",email : "shreyashkolhe2001@gmail.com",role : "front-end",active : true,})const result = await PlayList.insertMany([FirstStudent]);console.log("Data inserted successfully!");console.log(result);}catch(err){console.log(err)}}createDocument();
const playlistSchema = new mongoose.Schema ({// define schemaname:{type : String,required : true,unique : true,lowercase : true,trim : true,},sirname : String,email : String,role : String,active : Boolean,date : {type: Date,default : Date.now}})
Data inserted successfully! [ { name: 'shreyash', sirname: 'KOlhe', email: 'shreyashkolhe2001@gmail.com', role: 'front-end', active: true, _id: new ObjectId("61ce9f16de4f824303a544c2"), date: 2021-12-31T06:11:34.823Z, __v: 0 } ]
Post a Comment