//连接数据库
//引入mongoose
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/play",{ useUnifiedTopology: true,useNewUrlParser: true })
    .then(() =>console.log('连接成功'))
    .catch(err => console.log(err,'连接失败'));
//创建集合规则
const goodClass = new mongoose.Schema({
    name:String,
    age:Number,
    sex:String
});
//使用规则创建集合 返回的还是构造函数
// goodBoy 集合名称
const goodBoy = new mongoose.model("goodBoy",goodClass);

//创建数据
const sex = new goodBoy({
    name:"778",
    age:20,
    sex:"男"
});
//添加到数据库中
sex.save();
Last modification:March 19th, 2020 at 09:51 pm