当前位置:网站首页>Schema and model

Schema and model

2022-07-05 20:17:00 Why not sell egg cakes well

16 Schema and Model

Schema

Model

Document

From top to bottom

var mongoose=require("mongoose");

mongoose.connect('mongodb:// database ip Address : Port number / Database name ',{
    userMongoClient:true});

mongoose.connection.once("open",function(){
    
	console.log(" Database connection successful ");
});

// Create a schema object 
var Schema=mongoose.Schema;

var stuSchema=new Schema({
    
    
    title:String,
    age:Number,
    gender:{
    
        type:String,
        default:"female"
    },
    address:String
});

//model For the assembly , adopt model To operate on the database 
var StuModel=mongoose.model('Student',stuSchema);

// Insert a document into the database 
StuModel.create({
    
    name:" The Monkey King ",
    age"18,
    gender:"male",
    address:" Huaguoshan "
},function(err){
    
    if(!err){
    
        console.log(" Insert the success ");
    }
});

Creating models ( Collection name , Constrain objects )

Model operation database

原网站

版权声明
本文为[Why not sell egg cakes well]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207052011417013.html