const mongoose = require("mongoose");
mongoose.connect(
"mongodb://demoAdmin:123456@localhost:27017/demo",
{
useNewUrlParser: true,
useUnifiedTopology: true
},
err => {
if (err) {
console.log("数据库连接失败");
return;
}
console.log("数据库连接成功");
}
);
const UserSchema = mongoose.Schema({
name: String,
age: Number,
status: {
type: Number,
default: 1
}
});
const User = mongoose.model("User", UserSchema, "user");
// 新增
const user = new User({
name: "LittleFinger",
age: 33
});
user.save(err => {
if (err) {
console.log(err);
return;
}
console.log("数据新增成功");
});
网友评论